Java data types specify the type of value that a variable can store. When you create a variable in Java, you need to tell Java what kind of data the variable will contain.
For example, int age = 25; declares an integer variable named age.
Java data types are mainly divided into two categories:
- Primitive Data Types
- Non-Primitive Data Types
Primitive Data Types
Java has 8 primitive data types. They are used to store simple values directly.
| Data Type | Size | Example | Used For |
|---|---|---|---|
byte | 8 bits | 100 | Small whole numbers |
short | 16 bits | 10000 | Whole numbers |
int | 32 bits | 50000 | Common whole numbers |
long | 64 bits | 5000000000L | Large whole numbers |
float | 32 bits | 10.5f | Decimal values |
double | 64 bits | 10.5 | Precise decimal values |
char | 16 bits | 'A' | Single characters |
boolean | — | true | True or false values |
Integer Data Types
Java provides four types for storing whole numbers: byte, short, int, and long.
The byte type stores small integer values. Its range is -128 to 127.
The short type stores values from -32,768 to 32,767.
The int type is the most commonly used integer type. It can store values from -2,147,483,648 to 2,147,483,647.
The long type is used for very large integer values.
For example:
public class Main {
public static void main(String[] args) {
int age = 25;
long population = 5000000000L;
System.out.println(age);
System.out.println(population);
}
}
Notice the L after the large number. It indicates a long literal.
Floating-Point Data Types
Java provides float and double for storing decimal numbers.
A float uses 32 bits, while a double uses 64 bits and generally provides greater precision.
For example, float price = 99.5f; and double salary = 45000.75;.
double is generally preferred for ordinary decimal calculations.
Character Data Type
The char type is used to store a single UTF-16 code unit.
For example, char grade = 'A';.
Characters are written inside single quotation marks.
public class Main {
public static void main(String[] args) {
char grade = 'A';
System.out.println(grade);
}
}
Boolean Data Type
The boolean type stores one of two values: true or false.
For example, boolean isPassed = true;.
It is commonly used in conditions and decision-making statements.
Non-Primitive Data Types
Non-primitive data types are also called reference types. Unlike primitive types, they refer to objects or other reference values.
Common examples include:
| Type | Example |
|---|---|
String | String name = "Karim"; |
| Array | int[] numbers = {1, 2, 3}; |
| Class | Student student = new Student(); |
| Interface | Reference to an implementing object |
String is one of the most commonly used reference types in Java.
For example:
public class Main {
public static void main(String[] args) {
String name = "Karim";
System.out.println(name);
}
}
Primitive vs Non-Primitive Data Types
| Primitive | Non-Primitive |
|---|---|
| Built into Java | Created from classes, interfaces, arrays, etc. |
| Stores basic values | Holds references to objects or other reference values |
| 8 types | Many types |
Examples: int, double, char | Examples: String, arrays, classes |