using primitive types in java

problem

You wish to learn the primitive types of Java programming language. In this post we will see a list of the types and examples.

SOLUTION

Java’s type system offers a list of data types that we can use out of the box, the primitive types.

Java is a statically-typed language meaning we must first declare a variable before it can be used. For example:

				
					int month = 12;
				
			

Now we can use this variable straightaway. Since it’s an int we can use any mathematical operator +, -, *, /, %. The value 12 is stored in RAM in an easy way, we don’t have to worry as programmers.

primitive types
  • char
  • short
  • int
  • long
  • float
  • double
  • boolean
example usage
				
					char ch = 'x'; //a single character in single quotes
				
			
				
					short num = 125;
				
			
				
					int num = 12567;

				
			
				
					long longNumber = 34534311L; //good to have the L at the end to distinguish from double
				
			
				
					float floatingPointNumber = 3.14159268f; //requires the f at the end
				
			
				
					double doublePrecisionNumber = 12983.228;
				
			
				
					boolean isValid = true; //true or false value
				
			

conclusion

In this post we saw the primitive types of Java and how to use them/ initialize them.

Share it!

Facebook
Twitter
LinkedIn
Reddit
Picture of Ellion

Ellion

Professional IT consultant, writer, programmer enthusiast interested in all sorts of coding.
Eats all cookies 🍪

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x