In Java, data types define what kind of value a variable can store and how much memory it uses.
Java data types are mainly divided into:
Primitive Data Types
Number types store numeric values. They are divided into:
Type |
Size |
Example |
Range |
byte |
1 byte |
100 |
-128 to 127 |
short |
2 bytes |
5000 |
-32,768 to 32,767 |
int |
4 bytes |
100000 |
-2,147,483,648 to 2,147,483,647 |
long |
8 bytes |
10000000000L |
-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
Example:
int age = 25;
long population = 8000000000L;
👉 Use int for most whole numbers.
👉 Use long for very large numbers.
B. Floating-Point Types (Decimal Numbers)
Type |
Size |
Example |
Range |
float |
4 bytes |
3.14f |
±1.4E-45 to ±3.4E+38. |
double |
8 bytes |
3.14159 |
-1.7976931348623157E+308 to 1.7976931348623157E+308 |
Example:
float price = 19.99f;
double pi = 3.1415926535;
👉 double is more precise than float.
👉 Decimal numbers default to double.