DEV Community

Cover image for Java Data Types Made Simple: Primitive vs Non-Primitive Explained
Vinayagam
Vinayagam

Posted on

Java Data Types Made Simple: Primitive vs Non-Primitive Explained

Understanding Java Data Types: Primitive and Non-Primitive

Java data types define the type of data a variable can store in a program. They help the compiler allocate memory efficiently, ensure type safety, and improve overall program reliability.

Java is a statically typed language, which means every variable must be declared with a data type before it is used.


Basic Syntax

dataType variableName = value;
Enter fullscreen mode Exit fullscreen mode

Example:

int age = 21;
Enter fullscreen mode Exit fullscreen mode

Why Data Types Are Important?

  • Memory Allocation → Determines how much memory is required
  • Operation Support → Defines allowed operations
  • Type Safety → Prevents invalid assignments
  • Default Values → Each data type has a default value

Types of Data Types in Java

Java provides two main categories:

  1. Primitive Data Types
  2. Non-Primitive (Reference) Data Types

1. Primitive Data Types

Primitive data types are the most basic types in Java. They store actual values directly in memory and have a fixed size.

Primitive Data Types in Java (Detailed Explanation)

Java provides 8 primitive data types. These are used to store simple values directly in memory and are the foundation of all Java programs.

1. boolean Data Type

The boolean data type represents only two values: true or false. It is mainly used in decision-making statements like if, while, and loops.

Syntax:

boolean isActive;
Enter fullscreen mode Exit fullscreen mode

Example:

public class Main {
    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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Is Java fun? true
Is fish tasty? false
Enter fullscreen mode Exit fullscreen mode

2. byte Data Type

The byte data type is an 8-bit signed integer. It is useful for saving memory in large arrays.

Syntax:

byte value;
Enter fullscreen mode Exit fullscreen mode

Example:

public class Main {
    public static void main(String[] args) {
        byte age = 25;
        byte temperature = -10;

        System.out.println("Age: " + age);
        System.out.println("Temperature: " + temperature);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Age: 25
Temperature: -10
Enter fullscreen mode Exit fullscreen mode

3. short Data Type

The short data type is a 16-bit signed integer used when values are within a moderate range.

Syntax:

short number;
Enter fullscreen mode Exit fullscreen mode

Example:

public class Main {
    public static void main(String[] args) {
        short students = 1000;
        short temp = -200;

        System.out.println("Number of Students: " + students);
        System.out.println("Temperature: " + temp);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Number of Students: 1000
Temperature: -200
Enter fullscreen mode Exit fullscreen mode

4. int Data Type

The int data type is the most commonly used integer type in Java. It is a 32-bit signed integer.

Syntax:

int number;
Enter fullscreen mode Exit fullscreen mode

Size: 4 bytes (32 bits)

Example:

public class Main {
    public static void main(String[] args) {
        int population = 2000000;
        int distance = 150000000;

        System.out.println("Population: " + population);
        System.out.println("Distance: " + distance);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Population: 2000000
Distance: 150000000
Enter fullscreen mode Exit fullscreen mode

5. long Data Type

The long data type is a 64-bit signed integer, used for very large values.

Syntax:

long number;
Enter fullscreen mode Exit fullscreen mode

Size: 8 bytes (64 bits)

Example:

public class Main {
    public static void main(String[] args) {
        long worldPopulation = 7800000000L;
        long lightYears = 9460730472580800L;

        System.out.println("World Population: " + worldPopulation);
        System.out.println("Light Years: " + lightYears);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

World Population: 7800000000
Light Years: 9460730472580800
Enter fullscreen mode Exit fullscreen mode

6. float Data Type

The float data type is a 32-bit floating-point type used for decimal values with less precision.

Syntax:

float number;
Enter fullscreen mode Exit fullscreen mode

Size: 4 bytes (32 bits)

Example:

public class Main {
    public static void main(String[] args) {
        float pi = 3.14f;
        float gravity = 9.81f;

        System.out.println("Pi: " + pi);
        System.out.println("Gravity: " + gravity);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Pi: 3.14
Gravity: 9.81
Enter fullscreen mode Exit fullscreen mode

7. double Data Type

The double data type is a 64-bit floating-point type and is the default for decimal values.

Syntax:

double number;
Enter fullscreen mode Exit fullscreen mode

Size: 8 bytes (64 bits)

Example:

public class Main {
    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);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Pi: 3.141592653589793
Avogadro's Number: 6.02214076E23
Enter fullscreen mode Exit fullscreen mode

8. char Data Type

The char data type is used to store a single character. It uses Unicode and supports multiple languages.

Syntax:

char letter;
Enter fullscreen mode Exit fullscreen mode

Size: 2 bytes (16 bits)

Example:

public class Main {
    public static void main(String[] args) {
        char grade = 'A';
        char symbol = '$';

        System.out.println("Grade: " + grade);
        System.out.println("Symbol: " + symbol);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Grade: A
Symbol: $
Enter fullscreen mode Exit fullscreen mode

2.Non-Primitive (Reference) Data Types in Java

Unlike primitive data types, non-primitive data types do not store actual values directly. Instead, they store references (memory addresses) that point to objects.

These types are more powerful and are used to build real-world applications using Object-Oriented Programming (OOP).


1. String

A String represents a sequence of characters enclosed in double quotes. In Java, strings are objects and are immutable, meaning their value cannot be changed after creation.

Syntax:

```java id="p7t4yr"
String str = "Hello";




**Example:**



```java id="v2h5xq"
public class Main {
    public static void main(String[] args) {
        String name = "Geek1";
        String message = "Welcome to Java";

        System.out.println("Name: " + name);
        System.out.println("Message: " + message);
    }
}
Enter fullscreen mode Exit fullscreen mode

Output:

Name: Geek1
Message: Welcome to Java
Enter fullscreen mode Exit fullscreen mode

Note: Strings cannot be modified after creation. For heavy modifications, use StringBuilder.


2. Class

A class is a user-defined blueprint used to create objects. It defines properties (variables) and behaviors (methods).

Example:

```java id="3n5k9p"
class Car {
String model;
int year;

Car(String model, int year) {
    this.model = model;
    this.year = year;
}

void display() {
    System.out.println(model + " " + year);
}
Enter fullscreen mode Exit fullscreen mode

}

public class Main {
public static void main(String[] args) {
Car myCar = new Car("Toyota", 2020);
myCar.display();
}
}




**Output:**



```id="5l2m1n"
Toyota 2020
Enter fullscreen mode Exit fullscreen mode

3. Object

An object is an instance of a class. It represents real-world entities and contains:

  • State → Data (variables)
  • Behavior → Methods
  • Identity → Unique reference

Example:

```java id="8q1z9w"
class Car {
String model;
int year;

Car(String model, int year) {
    this.model = model;
    this.year = year;
}
Enter fullscreen mode Exit fullscreen mode

}

public class Main {
public static void main(String[] args) {
Car myCar = new Car("Honda", 2021);

    System.out.println("Model: " + myCar.model);
    System.out.println("Year: " + myCar.year);
}
Enter fullscreen mode Exit fullscreen mode

}




**Output:**



```id="1a2b3c"
Model: Honda
Year: 2021
Enter fullscreen mode Exit fullscreen mode

4. Interface

An interface defines a contract that classes must follow. It contains abstract methods that must be implemented.

It helps achieve abstraction and supports multiple inheritance in Java.

Example:

```java id="k4p8w2"
interface Animal {
void sound();
}

class Dog implements Animal {
public void sound() {
System.out.println("Woof");
}
}

public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
dog.sound();
}
}




**Output:**



```id="z8y7x6"
Woof
Enter fullscreen mode Exit fullscreen mode

5. Array

An array is used to store multiple values of the same type in a single structure. Arrays in Java are objects, dynamically allocated, and indexed starting from 0.

Example:

```java id="j9v2t6"
public class Main {
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]);
}
Enter fullscreen mode Exit fullscreen mode

}




**Output:**



```id="l0m9n8"
First number: 1
Second name: Geek2
Enter fullscreen mode Exit fullscreen mode

References

Top comments (0)