Like other programming languages, Java has the common data types such as int, float etc. They are grouped under two category, they are:
- Primitive Data types
- Non-primitive data types
Primitive Data types
The primitive data types are: boolean, char, byte, short, int, long, float and double.
Non-primitive data types
The non-primitive data types are Classes, Interfaces, and Arrays.
Let’s see the primitive data types in detail
Boolean Data Type
The Boolean data type can store only two possible values: True and False. The Boolean data type has one bit of information, but its “size” can’t be defined precisely. Example: Boolean 1 = True
Byte Data Type
It is an 8-bit signed two’s complement integer. Its value-range lies between -128 to 127 (inclusive). Its minimum value is -128 and maximum value is 127 and it has a default value of 0.
Short Data Type
The short data type is a 16-bit signed two’s complement integer. Its value-range lies between -32,768 to 32,767 (inclusive). Its minimum value is -32,768 and maximum value is 32,767 and it has a default value of 0.
The short data type can also be used to save memory just like byte data type. A short data type is 2 times smaller than an integer. Example: short s = 10000, short r = -30000
int Data Type
The int data type is a 32-bit signed two’s complement integer. Its value-range lies between – 2,147,483,648 (-2^31) to 2,147,483,647 (2^31 -1) (inclusive). Its minimum value is – 2,147,483,648and maximum value is 2,147,483,647 and it has default value of 0.
The int data type is most commonly used data type unless the memory concerns.
Example: int a = 500000, int b = -600000
long Data Type
The long data type is a 64-bit two’s complement integer. Its value-range lies between -9,223,372,036,854,775,808(-2^63) to 9,223,372,036,854,775,807(2^63 -1)(inclusive). It has minimum value of – 9,223,372,036,854,775,808and maximum value of 9,223,372,036,854,775,807 and the default value has 0. The long data type is used when you need a range of values more than that of int.
Example: long a = 100000L, long b = -200000L
Float Data Type
The float data type is a single-precision 32-bit IEEE 754 floating point. Its value range is unlimited. Float is used when you need to save memory in large arrays of floating point numbers. The float data type should never be used to store precise values. Its default value is 0.0F.
Example: float f1 = 255.5f
Double Data Type
The double data type is a double-precision 64-bit IEEE 754 floating point. Its value range is unlimited. The double data type is generally used for decimal values just like float. Like the float data type, the double data type also should not be used for precise values. Its default value is 0.0d.
Example: double d1 = 45.3
Char Data Type
The char data type is a single 16-bit Unicode character. Its value-range lies between ‘\u0000’ (or 0) to ‘\uffff’ (or 65,535 inclusive).The char data type is used to store characters.
Example: char Letter = 'X'
Strings
The string data type is used to store a sequence of characters (text). String values should be surrounded by double quotes (“string”).
Sample program for the data types in Java
public class main { public static void main(String args[]) { int myInteger = 57; System.out.println(myInteger); float X = 5.99f; System.out.println(X); char firstLetter = 'G'; System.out.println(firstLetter); boolean a= true; System.out.println(a); String text = "Hello"; System.out.println(text); } }
Output
57
5.99
G
true
Hello