Disclosure: This article may contain affiliate links. When you purchase, we may earn a commission.

8 Examples of Primitive Data Types In Java (int, long, boolean, float, double, byte, char, and short)

Hello guys, Data types are first few things you should learn when you start learning a programming language and when it comes to learn Java, there are 8 primitive data types which you should know. These are divided into three categories, numeric, text, and boolean values. Numeric data types can be further divided into two like natural numbers and floating pointing numbers. But, Before we get to a list of the 10 examples of primitive data types in Java, let me tell you a little bit more about what the primitive data types are.  There are essentially 8 primitive data types in Java. They are int, byte, short, long, float, double, boolean, and char. The primitive data types are not considered objects and represent raw values. These primitive data types are also stored directly on the stack.



8 Examples Of Primitive Data Types In Java

In Java, int data type is used for a 32-bit integer value, char is used for a 16-bit character, boolean is used for a true or false value, short is used for a 16-bit integer value, and so on and so forth. 

In this list, we have compiled all of the primitive data types in Java along with some examples. Keep reading to find out more. 

1. int

int is actually one of the most commonly used primitive data types in Java. It is also known as an integer and can hold a wide range of non-fractional number values. What you need to understand is that Java stores int using 32 bits of memory. 

What this means is that it can represent values from -2,147,483,648 (-2^31) to 2,147,483,647 (2^31-1). Sounds amazing, right?

It is very simple to declare an int in Java. 

int x = 424_242;

int y;

By default, the value of an unassigned int will be 0. It is also possible to define an int in a method. But then, you must assign a value to the variable before you can use it.  You can also easily perform any of the arithmetic operations on int. But you need to keep in mind that decimal values will be removed from the variable value when you perform calculations. 

2. byte

A byte is very similar to an int. The key difference is that a byte only takes up 8 bits of memory. As you can see, we call it a byte because it is basically made up of 8 bits. Since the memory size of a byte is really small, it can only store values between -128 (-2^7) and 127 (2^7 – 1).

It is very easy to declare a byte in Java:

byte b = 100;

byte empty;

primitive data types in Java with range and size


3. short

You can think of short as basically a compromise between int and byte. A short is made up of 16 bits of memory; so it is larger than a byte but smaller than an int. A short is able to store values from -32,768(-2^15) to 32,767(2^15 – 1). You can declare a short in Java like this:

short s = 20_020;

short s;

The default value of a short is also 0.

4. long

A long is another primitive data type that is related to int. You can think of long as basically the big brother of int. A long makes use of a massive 64 bits of memory. This makes it possible for long to hold a larger set of possible values.  

A long can store values between -9,223,372,036,854,775,808 (-2^63) and 9,223,372,036,854,775,807 (2^63 – 1).

The default value of a long is also 0. You can declare a long in Java with the following syntax:

long l = 1_234_567_890;

long l;
8 Examples Of Primitive Data Types In Java




5. float

The float type is used to represent basic fractional numbers in Java. It is very precise up to 6 decimal points. After that. the number can become less precise and more of an estimate. 

Just like int, a float is also stored in 32 bits of memory. But since it deals with decimal points, the range of a float is different from an int. It can store values between 1.40239846 x 10-45, and 3.40282347 x 1038.

You can see below how to declare a float type in Java:

float f = 3.45f;

float f;

6. double

A double type in Java can be seen as the big brother of the float type. It is stored in 64 bits of memory and offers double the precision in the case of decimal numbers. It can represent a much larger range of possible numbers. But, the precision of a float is not unlimited. 

The range of a double type in Java is between  4.9406564584124654 x 10-324 and 1.7976931348623157 x 10308. That range can also be positive or negative.

It is very easy to declare a double in Java:

double d = 3.13457599923384753929348D;

double d;

Just like float, the default value of a double type is 0.0. 

how to convert primitive data types in Java


7. boolean

A boolean is actually one of the most simple primitive data types in Java. As you may already know, a boolean can contain only 2 values: true or false. A boolean is stored in just one bit of data. But, for convenience, Java stores a boolean in a single byte instead of just a bit.  It is very easy to declare a boolean in Java:

boolean b = true;

boolean b;

The default value of a boolean is false. The boolean type is actually the cornerstone of controlling the flow of programs. You can also use the boolean type on other operators. 

8. char 

Now we come to the final entry in the list of primitive data types in Java: char. It is also called a character and is stored in 16 bits of memory that represent a Unicode-encoded character. The range of a char type is from 0 to 65,535. This represents \u0000' to ‘\uffff' in Unicode. You can declare a char in Java with the following syntax:

char c = 'a';

char c = 65;

char c;

When you are defining variables in Java, you can use any literal character and it will automatically get transformed into the respective Unicode encoding. The default value of a character type is /u0000'. 



Conclusion

That's all about the 8 essential data types in Java. It's must for every Java developer to not just know about these data types but also how and when to use them. You should also know what are their size like how many bits or bytes they take to store values as well as what are their maximum and minimum range. If you don't know these basic detail then you cannot choose the right data type for a given requirement  and not get the maximum performance you want in few cases. 

But, when you error, its better to error on big size like choosing long instead of int because that would only waste memory but not produce incorrect result. On contrary, if you choose a smaller type but your values are larger than they may overflow and you may get incorrect result. This is one of the very important point and that's why I am stressing that here. The best thing is to use the correct and most appropriate data type to write robust program and get best performance in java. 

There you have it. I am pretty sure that you have now covered all the basics regarding primitive data types in Java. If you liked this list of 8 examples of primitive data types in Java, feel free to share it with your friends and family. 

Other Java Programming articles you may like
  • How do you convert String to ASCII value in Java? (answer)
  • How to convert float to long in Java? (example)
  • How to convert a list to Stream in Java 8? (example)
  • How to convert double to long in Java? (double to long)
  • How do you convert Java Object to XML using JAXB? (example)
  • How to convert String to Boolean in Java? (string to boolean)
  • How to convert Fahrenheit to Celsius in Java? (program)
  • How to convert String to Integer in Java? (string to int)
  • String to Date conversion in Java using SimpleDateFormat class (solution)
  • 5 ways to convert InputStream to String in Java? (solution)
  • How do you format Date to String in Java? (answer)
  • XMLGregorianCalendar to Date in Java and Vice-versa (solution)
  • How do you convert Double to String in Java? (solution)
  • How do you convert Binary to Decimal in Java? (answer)

Thanks for reading this article so far. If you found this article worth reading then please share with your friends and colleagues. If you have any questions or doubts feel free to ask in comments. 

No comments:

Post a Comment

Feel free to comment, ask questions if you have any doubt.