DEV Community

Mohana Kumar
Mohana Kumar

Posted on

Mastering Java Data Types: Primitive vs. Non-Primitive Explained

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:

  1. Primitive Data Types
  2. 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;
Enter fullscreen mode Exit fullscreen mode

Range: -128 to 127

short

A 16-bit signed integer.

short year = 2025;
Enter fullscreen mode Exit fullscreen mode

Range: -32,768 to 32,767

int

A 32-bit signed integer and the most commonly used integer type.

int salary = 50000;
Enter fullscreen mode Exit fullscreen mode

long

A 64-bit signed integer used for very large numbers.

long population = 8000000000L;
Enter fullscreen mode Exit fullscreen mode

Note: Always append L at the end.


Floating-Point Types (Decimals)

float

A 32-bit decimal number.

float temperature = 36.5f;
Enter fullscreen mode Exit fullscreen mode

Note: Always append f at the end.

double

A 64-bit decimal number and the default choice for decimal values.

double price = 199.99;
Enter fullscreen mode Exit fullscreen mode

Character Type

char

Stores a single Unicode character.

char grade = 'A';
Enter fullscreen mode Exit fullscreen mode

Characters must be enclosed in single quotes.


Boolean Type

boolean

Stores only two values:

boolean isPassed = true;
boolean isAvailable = false;
Enter fullscreen mode Exit fullscreen mode

Example of Primitive Data Types

int age = 25;
double salary = 45000.50;
char grade = 'A';
boolean isPassed = true;
Enter fullscreen mode Exit fullscreen mode

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";
Enter fullscreen mode Exit fullscreen mode

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";
Enter fullscreen mode Exit fullscreen mode

Because String is a class, it provides many useful methods.

System.out.println(name.toUpperCase());
System.out.println(name.length());
Enter fullscreen mode Exit fullscreen mode

Output:

MOHANAKUMAR
11
Enter fullscreen mode Exit fullscreen mode

2. Arrays

Arrays store multiple values of the same type.

int[] marks = {90, 85, 95, 88};
Enter fullscreen mode Exit fullscreen mode

Access elements using an index:

System.out.println(marks[0]);
Enter fullscreen mode Exit fullscreen mode

Output:

90
Enter fullscreen mode Exit fullscreen mode

3. Classes

Classes are user-defined blueprints for creating objects.

class Student {
    String name;
    int age;
}
Enter fullscreen mode Exit fullscreen mode

Creating an object:

Student s1 = new Student();
s1.name = "Mohan";
s1.age = 21;
Enter fullscreen mode Exit fullscreen mode

4. Interfaces

Interfaces define a contract that classes must implement.

interface Animal {
    void sound();
}
Enter fullscreen mode Exit fullscreen mode

Classes can implement the interface:

class Dog implements Animal {
    public void sound() {
        System.out.println("Bark");
    }
}
Enter fullscreen mode Exit fullscreen mode

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"));
Enter fullscreen mode Exit fullscreen mode

Output:

java
true
Enter fullscreen mode Exit fullscreen mode

Primitive vs. Non-Primitive Example

int age = 25;              // Primitive
String name = "Mohan";     // Non-Primitive
Enter fullscreen mode Exit fullscreen mode
  • age stores the value 25 directly.
  • name stores 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)