Refreshing your ideas and broadening your visions
Data is an important part of program. All processing in an program is done upon data. That is, when program is running, it should hold some data temporarily in memory. This data is stored in variables. So, Variables are meant for storing data.
Variables will have some Data type, and that is the type of data that the program will store.
There are some pre-defined types available in Java. When we are declaring any variable to store data, we have to choose the correct Data type that we need.
Below image shows the list of Primitive Datatypes provided by Java
Primitive data types are categorized as:
byte: byte takes just 1 byte of memory and ranges from -128 to 127. Its default value is 0.
short: short takes 2 bytes of memory and ranges from -32768 to 32767. Its default value is 0.
int: int takes 4 bytes of memory and ranges from -2147483648 to 2147483647. Its default value is 0.
long: long takes 8 bytes of memory and ranges from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807. Its default value is 0.
float: float takes 4 bytes of memory and stores fractional numbers up to 7 decimal digits. Its default value is 0.0f
double: double takes 8 bytes of memory and stores fractional numbers up to 16 decimal digits. Its default value is 0.0d
char: char takes 2 bytes of memory and stores single character/letter or ASCII values. Its default value is \u0000
boolean: boolean takes 1 bit of memory and store true or false values. Its default value is false.
Java provides Wrapper classes for the above primitive types. We can use these wrapper classes to check the range, size, etc
The Wrapper classes are as follows:
We can check the class members and methods of these classes using javap command or check the Java source code.
Program to check the data type range and size:
public class CheckDataType { public static void main(String[] args) { System.out.println("int min range: " + Integer.MIN_VALUE); System.out.println("int max range: " + Integer.MAX_VALUE); System.out.println("int size: " + Integer.BYTES); System.out.println(""); System.out.println("float min range: " + Float.MIN_VALUE); System.out.println("float max range: " + Float.MAX_VALUE); System.out.println("float size: " + Float.BYTES); System.out.println(""); System.out.println("double min range: " + Double.MIN_VALUE); System.out.println("double max range: " + Double.MAX_VALUE); System.out.println("double size: " + Double.BYTES); } }
Output:
We can use the above variables to check the range and size of all primitive Java types, except Boolean. As Boolean have constants for True/False.