Java Data Types
Java is a statically typed programming language, meaning variable types are known at compile time. The compiler ensures type correctness, preventing assignments like int x = "GfG";, which would cause a compile-time error.
Data types in Java define the kind of data a variable can hold and the memory required to store it. They are broadly divided into two categories:
- Primitive Data Types: Store simple values directly in memory.
- Non-Primitive (Reference) Data Types: Store memory references to objects.

Primitive Data Types
Primitive types are the fundamental data types that store single values. Java defines eight primitive data types, summarized below:
| Type | Description | Default | Size | Example | Range |
|---|---|---|---|---|---|
| boolean | Logical values | false | JVM-dependent (typically 1 byte) | true, false | — |
| byte | 8-bit signed integer | 0 | 1 byte | 10 | -128 to 127 |
| char | 16-bit Unicode character | \u0000 | 2 bytes | 'A', '\u0041' | 0 to 65,535 |
| short | 16-bit signed integer | 0 | 2 bytes | 2000 | -32,768 to 32,767 |
| int | 32-bit signed integer | 0 | 4 bytes | 1000, -500 | -2,147,483,648 to 2,147,483,647 |
| long | 64-bit signed integer | 0L | 8 bytes | 123456789L | ±9.22e18 |
| float | 32-bit floating point | 0.0f | 4 bytes | 3.14f | ~6–7 digits precision |
| double | 64-bit floating point | 0.0d | 8 bytes | 3.14159d | ~15–16 digits precision |
1. boolean Data Type
Represents one of two logical values: true or false. Commonly used for conditional checks.
Syntax:
boolean booleanVar;
public class Geeks {
public static void main(String[] args) {
boolean isJavaFun = true;
boolean isFishTasty = false;
System.out.println("Is Java fun? " + isJavaFun);
System.out.println("Is fish tasty? " + isFishTasty);
}
}
Output
Is Java fun? true Is fish tasty? false
2. byte Data Type
An 8-bit signed integer used to save memory in large numeric arrays.
Syntax:
byte byteVar;
Size : 1 byte (8 bits)
public class Geeks {
public static void main(String[] args) {
byte age = 25;
byte temperature = -10;
System.out.println("Age: " + age);
System.out.println("Temperature: " + temperature);
}
}
Output
Age: 25 Temperature: -10
3. short Data Type
A 16-bit signed integer often used when memory is limited and values are moderate in size.
Syntax:
short shortVar;
Size : 2 bytes (16 bits)
public class Geeks {
public static void main(String[] args) {
short students = 1000;
short temp = -200;
System.out.println("Students: " + students);
System.out.println("Temperature: " + temp);
}
}
Output
Number of Students: 1000 Temperature: -200
4. int Data Type
A 32-bit signed integer commonly used for whole numbers.
Syntax:
int intVar;
Size : 4 bytes ( 32 bits )
public class Geeks {
public static void main(String[] args) {
int population = 2000000;
int distance = 150000000;
System.out.println("Population: " + population);
System.out.println("Distance: " + distance);
}
}
Output
Population: 2000000 Distance: 150000000
5. long Data Type
A 64-bit signed integer used when int is not sufficient for large values.
Syntax:
long longVar;
Size : 8 bytes (64 bits)
public class Geeks {
public static void main(String[] args) {
long worldPopulation = 7800000000L;
long lightYears = 9460730472580800L;
System.out.println("World Population: " + worldPopulation);
System.out.println("Light Years: " + lightYears);
}
}
Output
World Population: 7800000000 Light Year Distance: 9460730472580800
6. float Data Type
A 32-bit single-precision floating-point type used for fractional values.
Syntax:
float floatVar;
Size : 4 bytes (32 bits)
public class Geeks {
public static void main(String[] args) {
float pi = 3.14f;
float gravity = 9.81f;
System.out.println("Pi: " + pi);
System.out.println("Gravity: " + gravity);
}
}
Output
Value of Pi: 3.14 Gravity: 9.81
7. double Data Type
A 64-bit double-precision floating-point type and the default for decimal numbers.
Syntax:
double doubleVar;
Size : 8 bytes (64 bits). It is recommended to go through rounding off errors in java.
public class Geeks {
public static void main(String[] args) {
double pi = 3.141592653589793;
double avogadro = 6.02214076e23;
System.out.println("Pi: " + pi);
System.out.println("Avogadro's Number: " + avogadro);
}
}
Output
Value of Pi: 3.141592653589793 Avogadro's Number: 6.02214076E23
8. char Data Type
A 16-bit Unicode character used to store single symbols or letters.
Syntax:
char charVar;
Size : 2 bytes (16 bits)
Example: This example, demonstrates how to use char data type to store individual characters.
public class Geeks {
public static void main(String[] args) {
char grade = 'A';
char symbol = '$';
System.out.println("Grade: " + grade);
System.out.println("Symbol: " + symbol);
}
}
Output
Grade: A Symbol: $
Non-Primitive (Reference) Data Types
Non-primitive data types store references (memory addresses) rather than actual values. They are created by users and include types like String, Class, Object, Interface, and Array.
1. String
String represents a sequence of characters enclosed in double quotes. Unlike C/C++, Java strings are objects and are immutable.
Syntax:
String str = "Hello";
public class Geeks {
public static void main(String[] args) {
String name = "Geek1";
String message = "Welcome to Java";
System.out.println("Name: " + name);
System.out.println("Message: " + message);
}
}
Output
Name: Geek1 Message: Welcome to Java
Note: String cannot be modified after creation. Use StringBuilder for heavy string manipulation.
2. Class
A class is a user-defined blueprint that defines variables and methods. It represents a type of object and forms the foundation of Object-Oriented Programming.
class Car {
String model;
int year;
Car(String model, int year) {
this.model = model;
this.year = year;
}
void display() {
System.out.println(model + " " + year);
}
}
public class Geeks {
public static void main(String[] args) {
Car myCar = new Car("Toyota", 2020);
myCar.display();
}
}
Output
Toyota 2020
3. Object
An Object is an instance of a class representing real-world entities. It has state (data), behavior (methods), and identity (unique reference).
class Car {
String model;
int year;
Car(String model, int year) {
this.model = model;
this.year = year;
}
}
public class Geeks {
public static void main(String[] args) {
Car myCar = new Car("Honda", 2021);
System.out.println("Model: " + myCar.model);
System.out.println("Year: " + myCar.year);
}
}
Output
Car Model: Honda Car Year: 2021
4. Interface
An interface defines a contract of abstract methods that implementing classes must define. It provides a way to achieve abstraction and multiple inheritance in Java.
interface Animal {
void sound();
}
class Dog implements Animal {
public void sound() {
System.out.println("Woof");
}
}
public class Geeks {
public static void main(String[] args) {
Animal dog = new Dog();
dog.sound();
}
}
Output
Woof
5. Array
An array stores multiple elements of the same type in a single structure. Java arrays are objects, dynamically allocated, and indexed from 0.
public class Geeks {
public static void main(String[] args) {
int[] numbers = {1, 2, 3, 4, 5};
String[] names = {"Geek1", "Geek2", "Geek3"};
System.out.println("First number: " + numbers[0]);
System.out.println("Second name: " + names[1]);
}
}
Output
First Number: 1 Second Fruit: Geek2
Variables and Primitive DataTypes