Primitive Data Types in Java
Primitive data types in Java are predefined by the Java language and named as the reserved keywords. A primitive data type does not share a state with other primitive values. Java programming language supports the following eight primitive data types.
in this section, we will discuss all the Primitive data types in detail.
Primitive Number Types
Generally, the primitive number types are classified into two categories:
Whole numbers: The whole numbers hold the complete number, positive and negative, for example, 170, 225, -170, -225, etc. For these numbers, the valid data types are byte, short, int, and long. It depends on the number that which data type would be preferred.
Floating Numbers: The floating numbers are the numbers with a fraction part. These numbers have one or more decimal values, for example, 10.25, 15.25, etc. For these numbers, the valid data types are float and double.
From the all above data types, the int, double, and float are the most widely used data types.
Before understanding the Primitive data types, let's discuss data types in Java:
Data Types in Java
As its name specifies, data types are used to specify the type of data to store inside the variables. Java is a statically-typed language, which means all the variables should be declared before use. So, we have to specify the variable's type and name. A variable is declared as follows:
The above statement acknowledges your program that a file 'a' exists and holds integer type data with value 1. A variables data type specifies the type of value it contains.
Data types in Java categories into two categories:
A non-primitive data type can be a class, interface, and Array.
Let's back to our main topic, primitive data type; discuss each primitive data type in detail:
1) Boolean Data Type
A boolean data type can have two types of values, which are true and false. It is used to add a simple flag that displays true/false conditions. It represents one bit of information. It's is not a specific size data type. So, we can not precisely define its size.
2) Byte Data Type
3) Int Data Type
The int stands for Integer; it is a 32-bit signed two's complement integer. It's value can be from -2^31 to (2^31-1), which is -32,768 to 32,767 (inclusive). Its default value is zero. It represents an unsigned 32-bit integer, which has a value range from 0 to 32,767.
If memory saving is not our primary goal, then the int data type is used to define the integer value.
4) Long Data Type
It is a 64-bit 2's complement integer with a value range of (-2^63) to (2^63 -1) inclusive. It is used for the higher values that can not be handled by the int data type.
5) Float Data Type
The Float data type is used to declare the floating values ( fractional numbers). It is a single-precision 32-bit IEEE 754 floating-point data type.
Its value range is infinite. While declaring the floating, we must end the value with an f.
It is useful for saving memory in large arrays of floating-point numbers. It is recommended to use float data type instead of double while saving the floating numbers in large arrays, and not use it with precise numbers such as currency.
Note: A Scientific number can also be used to represent a scientific number with the power of 'e', where e represents the power of 10. for example, float f1= 25e2f; double d1= 15E2d; etc.
6) Double Data Type
The double data type is also used for the floating-point ( Fractional values) number. It is much similar to the float data type. But, generally, it is used for decimal values. Like the float data type, its value range is infinite and also can not be used for precise values such as currency.
The default value of the double data type is 0.0d. While declaring the double type values, we must end the value with a d.
7) Char Data Type
The char data type is also an essential primitive data type in Java. It is used to declare the character values. It is a single 16-bit Unicode Character with a value range of 0 to 65,535 (inclusive).
While declaring a character variable, its value must be surrounded by a single quote ('').
Note: The ASCII character values can also be used to display the characters.
8) Short Data Type
The short data type is also used to store the integer values. It is a 16-bit signed 2's complement integer with a value range of -32,768 to 32,767 (inclusive). It is also used to save memory, just like the byte data type.
It is recommended to use the short data type in a large array when memory saving is essential.
Here, we have discussed all the primitive data types in Java. Other data types such as Strings, Classes, Interfaces, and Arrays are non-primitive data types in Java.
However, Java provides support for character strings using the String class of Java.lang package. String class has some special support from the Java Programming language, so, technically it is a primitive data type. While using String class, a character string will automatically create a new String Object. For example, String s= " JavaTpoint is the best portal to learn Java";
Learn more about the String class in Java.
Default Values of Primitive data type In Java
In Java, it is not necessary to assign values while declaring. Every data type has some default values. If we do not assign a value to a data type, it will be initialized to the default values by the compiler.
Mostly, these values are null or 0 (Zero), depending on the data type. However, it is not recommended to leave the variables to their default values. If you have declared a variable, then initialize it with some value. Otherwise, do not declare it unnecessarily.
The below table is representing the data types with their default values: