Data type defines the values that a variable can take, for example if a variable has integer data type, it can only take integer values. In java we have two categories of data type: 1) Primitive data types 2) Non-primitive data types.
1) Primitive data types
In Java, we have eight primitive data types: boolean, char, byte, short, int, long, float and double. Java developers included these data types to maintain the portability of java as the size of these primitive data types do not change from one operating system to another.
byte, short, int and long data types are used for storing whole numbers.
float and double are used for fractional numbers.
char is used for storing characters(letters).
boolean data type is used for variables that holds either true or false.
byte: This can hold smallest whole number between -128 and 127. Mostly used to save memory and when you are certain that the numbers would be in the limit specified by byte data type.
Default size of this data type: 1 byte
byte num = 101;
short: This is greater than byte in terms of size and less than integer. Its range is -32,768 to 32767.
Default size of this data type: 2 byte
short num = 45678;
int: Used when short is not large enough to hold the number, it has a wider range: -2,147,483,648 to 2,147,483,647
Default size: 4 byte
int num = 123679;
long: Used when int is not large enough to hold the value, it has wider range than int data type, ranging from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
size: 8 bytes
long num = 788778877;
boolean: holds either true of false.
boolean b = true;
char: holds characters.
size: 2 bytes
char ch = ‘A’;
float: Sufficient for holding 6 to 7 decimal digits
size: 4 bytes
float num = 100.12f;
double: Sufficient for holding 15 decimal digits
size: 8 bytes
double num = 200.456d;
2) Non-Primitive data types
Non-Primitive data types includes String, Arrays etc. We have separate tutorials for each of them. You can find the links here.