Based on the data type of a variable, the operating system allocates memory and decides what type of value will be stored in the reserved memory. You can store integers, decimals, or characters in these variables.
The appropriate data type is to be select by the programmer according to the needs of the application.
In Java, Data types are divided into 2 categories:
Integer data type can hold whole numbers such as 10, 56, -97. Java supports 4 types of integers: byte, short, int, and long. Java does not support the concept of unsigned data types and therefor all Java values are signed means they can be positive or negative.
Type | Size | Suffix | Range |
---|---|---|---|
byte | 1 byte | y or Y | -128 to 127 |
short | 2 bytes | s or S | -32768 to 32767 |
int | 4 bytes | None | -231 to 231 |
long | 8 bytes | l or L | -263 to 263 |
Integer data type can hold only whole numbers, if you want to store decimal numbers than use floating point data type.
Type | Size | Suffix | Range |
---|---|---|---|
float | 4 byte | f or F | -231 to 231-1 |
double | 8 bytes | d or D | -263 to 263-1 |
Type | Size | Range |
---|---|---|
char | 2 byte | -215 to 215-1 |
boolean | 1 bytes | true/false |