Java Data Types
When learning Java, one of the most important concepts you’ll encounter early on is data types. In programming, a data type defines what kind of value a variable can hold and what operations can be performed on it. Since Java is a strongly typed language, every variable must have a declared data type before it can be used.
In this guide, we’ll explore the different data types in Java with examples to help you understand them better.
What are Data Types in Java?
A data type specifies the type of data a variable can store. For example, you might store:
- Numbers (like 10, 3.14)
- Characters (like ‘A’, ‘b’)
- Boolean values (true/false)
- Strings (like “Hello, Java!”)
Java data types are divided into two categories:
- Primitive Data Types
- Non-Primitive (Reference) Data Types
1. Primitive Data Types
Primitive data types are the basic building blocks of Java. They are predefined by the Java language and hold simple values. Java has 8 primitive data types:
Data Type | Size | Default Value | Example | Description |
---|---|---|---|---|
byte | 1 byte (8-bit) | 0 | byte age = 25; | Stores small integers from -128 to 127 |
short | 2 bytes (16-bit) | 0 | short year = 2025; | Stores integers from -32,768 to 32,767 |
int | 4 bytes (32-bit) | 0 | int salary = 50000; | Most commonly used for integers (-2^31 to 2^31-1) |
long | 8 bytes (64-bit) | 0L | long population = 8000000000L; | Stores very large integers (-2^63 to 2^63-1) |
float | 4 bytes (32-bit) | 0.0f | float pi = 3.14f; | Stores fractional numbers (single precision) |
double | 8 bytes (64-bit) | 0.0d | double price = 199.99; | Stores fractional numbers (double precision, more accurate) |
char | 2 bytes (16-bit, Unicode) | ‘\u0000’ | char grade = 'A'; | Stores a single character |
boolean | 1 bit (JVM dependent) | false | boolean isJavaFun = true; | Stores logical values: true or false |
Example: Using Primitive Data Types
public class PrimitiveExample {
public static void main(String[] args) {
byte age = 25;
short year = 2025;
int salary = 50000;
long population = 8000000000L;
float pi = 3.14f;
double price = 199.99;
char grade = ‘A’;
boolean isJavaFun = true;
System.out.println(“Age: ” + age);
System.out.println(“Year: ” + year);
System.out.println(“Salary: ” + salary);
System.out.println(“Population: ” + population);
System.out.println(“Pi: ” + pi);
System.out.println(“Price: ” + price);
System.out.println(“Grade: ” + grade);
System.out.println(“Is Java Fun? ” + isJavaFun);
}
}
2. Non-Primitive (Reference) Data Types
Non-primitive data types are objects in Java. They do not store the actual value directly but rather a reference (memory address) to the object.
Some common non-primitive types include:
- String – Stores sequences of characters.
- Arrays – Stores multiple values of the same type.
- Classes – User-defined types that can contain variables and methods.
- Interfaces – Abstract types used to define a contract.
Example: Using Non-Primitive Data Types
public class NonPrimitiveExample {
public static void main(String[] args) {
String message = “Hello, Java!”;
int[] numbers = {10, 20, 30, 40};
System.out.println(“Message: ” + message);
System.out.println(“First Number: ” + numbers[0]);
}
}
Primitive vs. Non-Primitive Data Types
Feature | Primitive Data Types | Non-Primitive Data Types |
---|---|---|
Defined by | Java language | Programmer (or Java library) |
Memory Storage | Stores actual values | Stores reference to object |
Examples | int, float, char, boolean | String, Array, Class, Interface |
Default Values | e.g., 0, 0.0, false | null |
Operations | Faster and more efficient | Rich functionality, more powerful |
Key Points to Remember
- Java is strongly typed, so you must declare a variable’s data type before use.
- Use int for most integer calculations unless you need very large numbers (then use long).
- Use double for most decimal values (float is less precise).
- Always append L to long values and f to float values.
- String is not a primitive type but is used almost like one because it’s so common.

Conclusion
Understanding data types in Java is essential because they form the foundation of all programming tasks. Whether you’re dealing with numbers, characters, or objects, knowing which data type to use ensures that your programs run efficiently and without errors.
By mastering both primitive and non-primitive data types, you’ll have a solid grasp of how Java manages and processes data — an essential step in becoming a confident Java programmer.