Scientech Easy Non-Primitive Data types in Java with Example | Scientech Easy

Saturday, May 18, 2019

Non-Primitive Data types in Java with Example

In the previous tutorial, we have learned the various data types in Java has been categorized into primitive and non-primitive data types. Primitive data types are those data types which are used by programmers when creating variables in their program.

For example, boolean, char, byte, short, int, long, float, and double. These eight data types are called primitive data types.
Now let's move one step ahead to learn another kind of datatypes, called non-primitive data types supported by Java.

Non-Primitive Data types & Reference Data types in Java

Non-primitive data types are created by programmers. They are not predefined in Java like primitive data types. When we define a variable of non-primitive data types, it references a memory location where data is stored in the heap memory. i.e, it references to a memory where an object is actually placed.

Therefore, the variable of a non-primitive data type is also called reference data types or object reference variable. This object reference variable lives on the stack memory and the object to which it points always lives on the heap memory. The stack holds a pointer to the object on the heap.
In Java programming, all non-primitive data types are simply called objects which are created by instantiating a class.

Key points:
1. The default value of any reference variable is null.
2. Whenever you will pass a non-primitive data type to a method, you are actually passing an address of that object where data is stored.

How to Declare Non-primitive type Data types in Java?

In primitive data type, we declare like this:
           int p=100; // p is an int data type which can store the only integer value.
In non-primitive datatypes, An object reference variable ( or simply called a reference variable) is declared just like we declare a primitive variable.
     School sc; 
In this example, School is the name of a class and "sc" is the name of a reference variable. No object has yet been created.

We create an object of a class using the new keyword. For example, the following statement creates an object of a class School and assigns it to the reference variable "sc".
       sc=new School();
      School ➞ name of the class.
             sc ➞ Object reference. An object reference is a variable which stores the addresses of the objects in the computer's memory. An object represents an instance through which we can access member.
   School() ➞ Constructor of the class. The constructor of a class which is generally used to initialize an object.
         new ➞ is a special keyword that creates the memory in the java.

Now an object of class School lives on the heap and the object reference variable "sc" refers to it. The declaration of an object reference variable, object creation, and initialization of reference variable can also be done in a single line statement like this:
        School sc=new School();

Let's understand a simple practical example program. In this example program, we will get address of the object as output which is stored in object reference variable on the stack memory.
Program source code 1:
    package scientecheasy; public class School{ // Declaration of a primitive variable. String name="RSVM"; // Instance variable public static void main(String[] args){ // Creating an object of the class. School sc=new School(); // sc is Non-primitive data type i.e Object REFERENCE. // Print the address of the memory location of an Object. System.out.println(sc); // Now we cannot access instance variable directly. we call instance variable by using reference variable sc Which is created above. System.out.println(; } }
    Output: School@1db9742 RSVM

Memory Allocation of Object & Object Reference Variable

From above example program, you will have understood that In Java, a variable whose type is a class, does not actually hold an object. Actually, It holds the memory location of an object. The object itself is stored elsewhere. In the below figure, you can see the memory location of object and object reference variable of above program.
Memory location of Object and Object reference variable
As shown in the above figure, Object reference variable 'sc' contains the address '1db9742' which is the address of the memory location of the object on the heap. On this address, Data is stored inside the heap memory. Creating an object means storing data in memory.

So, we can say that "sc" variable does not contain the object. It refers to the object.

Types of Non-primitive Data types

There are five types of non-primitive data types in Java. They are as follows:
1. Class
2. Object
3. String
4. Array
5. Interface
Types of non-primitive data types in java

These all non-primitive data types will be explained one by one in next tutorials.

Difference between Primitive and Non-primitive Data type in Java

1. Primitive data types are predefined in Java whereas Non-primitive data types are created by programmers. They are not predefined in Java.
2. In primitive data type, variables can store only one value at a time whereas, in non-primitive data type, we can store multiple values either the same type or different type or both.
3. All the data for the Primitive type variables are stored on the stack whereas, for reference types, the stack holds a pointer to the object on the heap. 

Final words
Hope that this tutorial has covered almost all the important points related to non-primitive data type in Java with examples program. I hope that you will have understood this tutorial and enjoyed it.
Thanks for reading!
NextMemory allocation of Primitive & Non-primitive Datatypes
⏪ Prev Next ⏩