,

Non-Primitive Data Types in Java


In Java, Data Types are categorized into Primitive and Non-Primitive Data Types. While Primitive Data Types are used for holding values, Non-Primitive Data Types are user-defined Data Types, used for holding references. I am discussing this in a separate article, as it requires a basic understanding of OOP, e.g., I have mentioned references here.

What are objects and references?

In Object-Oriented Programming, we try to view each piece of data as an Object, having certain characteristics and features. From objects with similar characteristics, we try to extract a template and use the template to create different objects with different values for those characteristics.

Let’s say we are trying to create a food delivery app, and for that, we need to store user details. We need name, email, address, contact no, etc. So, if we think of each user as an object, then these are the characteristics for each user, and these details will vary for each user, making them unique.

If we try to come up with a template of the same, like below, this template is called a Class. Different users created using the class are called objects. (For the time being, ignore the details; we will learn all these in more depth)

class User{
    String name;
    String email;
    String address;
    long phoneNumber;
    User(name, email, address, phoneNumber){
        this.name = name;
        this.email = email;
        this.address = address;
        this.phoneNumber = phoneNumbers
    }
}

User john = new User("John","john@gmail.com","Marine Drive",19893033);
User ram = new User("Ram","ram@gmail.com","Queen Street",9282937);

Like I said, variables declared here using user-defined Data Types(Class) hold the reference(memory location in Heap) of the Object, not the actual value, like name, email, etc.

In Java, below are a few different types of Non-Primitive Data Types

Whenever any object is created using these Data Types, they return a reference to the Object, and the variable holds the reference.

We will discuss these Data Types in a lot more detail. That’s all for now related to Data Types in Java. We will learn about Type conversion next.