If you're learning Java, understanding data types is one of the most important first steps. Java is a statically typed language, which means every variable must have a defined data type before it can be used.
Data types tell Java what kind of value a variable can store and what operations can be performed on it. Java divides data types into two main categories:
- Primitive Data Types
- Non-Primitive Data Types (Reference Types)
Let's explore both in detail.
Primitive vs. Non-Primitive Data Types
| Feature | Primitive Data Types | Non-Primitive Data Types |
|---|---|---|
| Storage | Store the actual value directly | Store a reference to an object |
| Size | Fixed size | Depends on the object |
| Methods | Cannot directly call methods | Can call methods |
| Null Value | Cannot be null | Can be null |
| Performance | Faster and memory efficient | Slightly more memory usage |
| Examples | int, double, char, boolean | String, Arrays, Classes, Interfaces |
Primitive Data Types
Primitive data types are the basic building blocks of Java. Java provides exactly 8 primitive data types, each designed to store a specific kind of value.
Integer Types (Whole Numbers)
byte
An 8-bit signed integer.
byte age = 25;
Range: -128 to 127
short
A 16-bit signed integer.
short year = 2025;
Range: -32,768 to 32,767
int
A 32-bit signed integer and the most commonly used integer type.
int salary = 50000;
long
A 64-bit signed integer used for very large numbers.
long population = 8000000000L;
Note: Always append L at the end.
Floating-Point Types (Decimals)
float
A 32-bit decimal number.
float temperature = 36.5f;
Note: Always append f at the end.
double
A 64-bit decimal number and the default choice for decimal values.
double price = 199.99;
Character Type
char
Stores a single Unicode character.
char grade = 'A';
Characters must be enclosed in single quotes.
Boolean Type
boolean
Stores only two values:
boolean isPassed = true;
boolean isAvailable = false;
Example of Primitive Data Types
int age = 25;
double salary = 45000.50;
char grade = 'A';
boolean isPassed = true;
Non-Primitive Data Types (Reference Types)
As applications become more complex, primitive types alone are not enough. Java uses non-primitive data types, also called reference types, to represent more sophisticated data structures and objects.
Instead of storing the actual data directly, a reference variable points to an object.
How Reference Types Work
When a reference type is created:
- The variable stores a reference.
- The actual object exists elsewhere in memory.
- Multiple variables can refer to the same object.
Example:
String name = "Mohanakumar";
Here, name stores a reference to a String object.
Common Non-Primitive Data Types
1. String
A String stores a sequence of characters enclosed in double quotes.
String name = "Mohanakumar";
Because String is a class, it provides many useful methods.
System.out.println(name.toUpperCase());
System.out.println(name.length());
Output:
MOHANAKUMAR
11
2. Arrays
Arrays store multiple values of the same type.
int[] marks = {90, 85, 95, 88};
Access elements using an index:
System.out.println(marks[0]);
Output:
90
3. Classes
Classes are user-defined blueprints for creating objects.
class Student {
String name;
int age;
}
Creating an object:
Student s1 = new Student();
s1.name = "Mohan";
s1.age = 21;
4. Interfaces
Interfaces define a contract that classes must implement.
interface Animal {
void sound();
}
Classes can implement the interface:
class Dog implements Animal {
public void sound() {
System.out.println("Bark");
}
}
Why Non-Primitive Types Are Powerful
Unlike primitive types, reference types can:
- Store complex data
- Contain multiple properties
- Provide built-in methods
- Support object-oriented programming concepts
- Be extended and reused
Example:
String name = "Java";
System.out.println(name.toLowerCase());
System.out.println(name.contains("va"));
Output:
java
true
Primitive vs. Non-Primitive Example
int age = 25; // Primitive
String name = "Mohan"; // Non-Primitive
-
agestores the value 25 directly. -
namestores a reference to a String object.
Conclusion
Data types are the foundation of Java programming.
Primitive Data Types are used to store simple values such as numbers, characters, and true/false values. They are fast, lightweight, and efficient.
Non-Primitive Data Types are used to store complex data structures and objects. They provide methods, support object-oriented programming, and make it possible to build real-world applications.
A strong understanding of both primitive and non-primitive data types will help you write cleaner, more efficient Java programs and prepare you for advanced concepts such as classes, objects, collections, and frameworks.
Top comments (0)