Data types in Java specify the kind of value a variable can hold and the operations that can be performed on it. Java supports two categories of data types: primitive and non-primitive (reference).
- Data types help the compiler detect incompatible assignments and operations.
- Fields and array elements receive default values, while local variables must be assigned a value before use.
Syntax
dataType variableName = value;
Types of Data Types
Java data types are classified into two categories:
- Primitive Data Types: Store primitive values such as integers, characters, and boolean values.
- Non-Primitive Data Types: Store references to objects rather than the objects themselves.

1. Primitive Data Types
Java provides eight primitive data types. They have well-defined value ranges or sets of values, but the storage size of boolean is implementation-dependent.
Data Type | Description | Size (bits) | Default Value | Example |
|---|---|---|---|---|
byte | Signed integer | 8 | 0 | 10 |
short | Signed integer | 16 | 0 | 2000 |
int | Signed integer | 32 | 0 | 100000 |
long | Large signed integer | 64 | 0L | 7800000000L |
float | Single-precision floating point | 32 | 0.0f | 3.14f |
double | Double-precision floating point | 64 | 0.0d | 3.14159 |
char | UTF-16 code unit | 16 | '\u0000' | 'A' |
boolean | Logical value | Not specified | false | true |
Note: The bit widths represent the defined value representation of numeric and character types, not necessarily their actual memory footprint. The storage size of boolean is implementation-dependent.
Example: Program to demonstrate all eight primitive data types.
class Geeks{
public static void main(String[] args){
// Integer types
byte age = 25;
short students = 1000;
int population = 2000000;
long worldPopulation = 7800000000L;
// Floating-point types
float gravity = 9.81f;
double pi = 3.141592653589793;
// Character type
char grade = 'A';
// Boolean type
boolean isPassed = true;
System.out.println("Age: " + age);
System.out.println("Students: " + students);
System.out.println("Population: " + population);
System.out.println("World Population: " + worldPopulation);
System.out.println("Gravity: " + gravity);
System.out.println("Value of Pi: " + pi);
System.out.println("Grade: " + grade);
System.out.println("Passed: " + isPassed);
}
}
Output
Age: 25 Students: 1000 Population: 2000000 World Population: 7800000000 Gravity: 9.81 Value of Pi: 3.141592653589793 Grade: A Passed: true
Explanation:
- byte, short, int, and long store whole numbers.
- float and double store floating-point numbers with different precision.
- char stores a UTF-16 code unit.
- boolean stores either true or false.
2. Non-Primitive (Reference) Data Types
Reference data types store references to objects rather than primitive values. Java supports both predefined and user-defined reference types.
- String: Represents an immutable sequence of characters.
- Class: Defines the fields and methods of an object.
- Interface: Defines a contract that implementing classes follow.
- Array: Stores a fixed number of elements of the same declared type.
Example: Program to demonstrate reference data types in Java.
// Interface
interface Vehicle{
void display();
}
// Class
class Car implements Vehicle{
String model = "Honda";
int year = 2021;
public void display(){
System.out.println("Car Model: " + model);
System.out.println("Car Year: " + year);
}
}
class Geeks {
public static void main(String[] args){
// String
String name = "GeeksforGeeks";
// Array
int[] numbers = {10, 20, 30};
// Class reference
Car myCar = new Car();
// Interface reference
Vehicle vehicle = myCar;
System.out.println("Name: " + name);
System.out.println("First Number: " + numbers[0]);
vehicle.display();
}
}
Output
Name: GeeksforGeeks First Number: 10 Car Model: Honda Car Year: 2021
Explanation:
- name refers to a String object.
- numbers refers to an integer array.
- myCar refers to an instance of the Car class.
- vehicle is an interface reference referring to the same Car object.
Primitive Vs Non-Primitive Data Types
Primitive Data Types | Non-Primitive Data Types |
|---|---|
Hold primitive values. | Hold object references. |
Predefined by Java. | Can be predefined or user-defined. |
Cannot hold null. | Can hold null. |
Do not provide instance methods. | Allow access to applicable object methods. |
Examples: int, char, float, boolean. | Examples: String, arrays, classes, interfaces. |