Reserved memory locations that are generally used to store locations are known as variables in Java. In simple words, whenever a variable is created, there is some space reserved in the memory. Now, the thing is that the operating system is responsible to allocate the memory and to decide what should be stored in the reserved memory, but all of this process is dependent on the Java data type of a variable. Hence, just by assigning different data types to the variables programmers can store characters, integers or decimals in these variables.
Here is the syntax of the Java data types along with an example:
int Num = 5; float FloatNum = 5.99f; char Letter = 'D'; boolean Bool = true; String Str = "Hello";
There are generally two types of data types in the Java that are depicted below:
The building blocks of data manipulation is known as primitive data type in the Java language. This data type is generally known as the most basic data types that are available in the Java language.
There are generally eight types of primitive data types that are depicted below:
Here is a table that is depicting the data type, default value and default size:
|Data Type||Default Value||Default size|
In order to store only two possible values that are true and false, the Boolean data type is used. Generally, this data type is used for the simple flags that basically track true/false conditions. The Boolean data type’s size can't be defined precisely and it is known to specify one bit of information.
boolean Bool = true;
boolean Bool1 = false;
boolean Bool = true; boolean Bool1 = false; System.out.println(Bool); System.out.println(Bool1);
The byte data type is basically called as an example of the primitive data type in Java language. It can be generally defined as the 8-bit signed two's complement integer. The value-range of the byte data type generally lies between -128 to 127. The minimum of byte data type is -128 and the maximum value is 127 and the default value is 0. The byte data type is basically used to save the memory in the large arrays that is where the memory savings is mostly required. The byte data type is known to save space as of the fact that a byte is 4 times smaller than an integer. This can also be a replacement of "int" data type.
byte Num = 10;
byte Num = 10; System.out.println(Num);
A 16-bit signed two's complement integer in Java language is known as the short data type. Generally the value-range of short data type lies between -32,768 to 32,767. The minimum value of short data type is -32,768 and the maximum value is 32,767 and the default value is 0. In order to save the memory just as same as the byte data type, the short data type is used. A short data type is generally known to be 2 times smaller than an integer.
short myNum = 5000;
short Num = 5000; System.out.println(Num);
A 32-bit signed two's complement integer in Java is known as an int data type. The value-range of the int data type lies between - 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1). The minimum value is - 2,147,483,648 and the maximum value is 2,147,483,647. Along with the default value 0. If there is no problem about the memory, then the int data type is basically used as a default data type for integral values.
int myNum = 100000;
int Num = 100000; System.out.println(Num);
A 64-bit two's complement integer in Java is known as the long data type. The value-range of long data type generally lies between -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1). The minimum value of long data type is - 9,223,372,036,854,775,808 and the maximum value is 9,223,372,036,854,775,807 along with a default value of 0. When there is a need of a range of values more than those that are provided by int the the long data type is used.
long myNum = 14000000000L;
long Num = 14000000000L; System.out.println(Num);
A single-precision 32-bit IEEE 754 floating point in Java is known as the float data type. This data type’s value range is known to be unlimited and it is generally recommended to use a float instead of double if the programmer needs to save memory in the large arrays of floating point numbers. The default value of the float data type is 0.0F.
float myNum = 5.75f;
float Num = 5.25f; System.out.println(Num);
A double-precision 64-bit IEEE 754 floating point in Java is known as the double data type. The value range of the double data type is known to be unlimited. Just like float the double data type is basically used for the decimal values. There is a catch in using the double data type as it should never be used for the precise values, like currency. The default value of the double data type is 0.0d.
double myNum = 19.99d;
double Num = 18.88d; System.out.println(Num);
A single 16-bit Unicode character in Java is known as the char data type. The char data type value-range basically lies between '\u0000' (or 0) to '\uffff'. In order to store the characters the char data type is used in Java.
char myGrade = 'B';
char Grade = 'D'; System.out.println(Grade);
Non-primitive data types are also known as the reference types as they generally refer to the objects.
Here is the main difference between the primitive and non-primitive data depicted below: