Java - Difference Between Primitive and Object Data Types

By Shahnail Khan Last updated : March 22, 2024

In Java, data types are categorized into two main groups: primitive data types and non-primitive (object) data types. Understanding the differences between these two types is crucial for Java developers. In this article, we'll understand the differences between these two in detail with relevant examples.

What are Primitive Data Types in Java?

Primitive data types are the most basic data types in Java. They are predefined by the language and represent single values. In Java, there are eight primitive data types: 

  • byte
  • short
  • int
  • long
  • float
  • double
  • char
  • boolean

These types directly store their values in memory.

Primitive Data Types Example in Java

Let's take an example to understand this better.

public class primitiveDataTypes {
  public static void main(String[] args) {
    // Example of primitive data types
    int age = 30; // Represents integer
    double height = 6.1; // Represents floating-point
    char grade = 'A'; // Represents a single character
    boolean isStudent = true; // Represents true or false (logical)
    
    // Displaying values
    System.out.println("Primitive Data Types:");
    System.out.println("Age: " + age);
    System.out.println("Height: " + height);
    System.out.println("Grade: " + grade);
    System.out.println("Is Student: " + isStudent);
  }
}

The output of the above example is:

Primitive Data Types:
Age: 30
Height: 6.1
Grade: A
Is Student: true

In this example, we have used four primitive data types, namely-

  • int: Stores whole numbers like age.
  • double: Stores decimal numbers like height.
  • char: Stores single characters like a grade.
  • boolean: Stores true or false values like whether someone is a student or not.

Each of these variables directly holds their respective values in memory without any additional references or objects.

What are Object Data Types in Java?

Object data types, also known as non-primitive or reference types, are derived from classes and are more complex than primitive data types. They do not directly store values; instead, they store references (memory addresses) to objects.

We know that Objects are instances of classes, which can contain both data (attributes) and methods (functions).

Example of Object Data Types in Java

public class Person {
  private String name;
  private int age;

  public Person(String name, int age) {
    this.name = name;
    this.age = age;
  }

  public String getName() {
    return name;
  }

  public int getAge() {
    return age;
  }

  public void setName(String name) {
    this.name = name;
  }

  public void setAge(int age) {
    this.age = age;
  }
  public static void main(String[] args) {
    // Creating an object of type Person
    Person obj = new Person("Rishab", 35);

    // Using object methods to access and modify its properties
    System.out.println("Name: " + obj.getName());
    System.out.println("Age: " + obj.getAge());

    obj.setName("Akash");
    obj.setAge(20);

    System.out.println("Updated Name: " + obj.getName());
    System.out.println("Updated Age: " + obj.getAge());
  }
}

The output of the above example is:

Name: Rishab
Age: 35
Updated Name: Akash
Updated Age: 20

In the above example,

  • We defined a Person class with properties name and age, and getter and setter methods to access and modify these properties.
  • In the Main class, we create an object of type Person named obj using the new keyword. This allocates memory for a Person object and returns a reference to it, which is stored in obj.
  • We then use the "obj" object to access its properties and modify them using its methods.

Difference Between Primitive and Object Data Types in Java

Sr. No. Primitive Data Types  Object Data Types 
1. Primitive data types store actual values. Object data types store references to objects.
2. Primitive data types limited in functionality. Object data types extensible and customizable through classes.
3. Primitive data types cannot be null. Object data types can be null.
4. Primitive data types cannot invoke methods. Object data types can invoke methods via objects.
5. Primitive data types have less memory overhead. Object data types have more memory overhead.
6. Primitive data types faster in performance. Object data types slower in performance.
7. Primitive data types operate directly on values. Object data types operate on references to objects.
8. Primitive data types cannot be extended or subclassed. Object data types can be extended and subclassed.
9. Primitive data types consist of simple types. Object data types consist of complex types.

Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.