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

Thursday, June 28, 2018

Non-Primitive Data types in Java with Example

In this tutorial, I will explain about Non-primitive Data types in Java with examples and program in very easy ways. I will explain about Reference data types or Object data types. After that, we will learn how to declare non-primitive data types? I always try to understand you in the better way. So Let's start one by one.

Non-Primitive Data types in Java

Non-primitive data types are created by the programmers. They are not predefined in Java like primitive data types.
➝ It is also called reference data types or object data types.
➝ A reference type references a memory location where data is stored in the heap memory.
For reference types, the stack holds a pointer to the object on the heap. 
➝ Reference data types are created using the constructor of the classes. i.e if you are creating a class and creating an object then it is called reference data types.
➝ The default value of any reference variable is null.

How to declare non-primitive type data types?

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 data type, we declare like this
            School sc=new School();
where 
           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 instance through which we can access member.
            School() ➞ Constructor of the class.
            new ➞ is a special keyword that creates the memory in the java.
Let's understand these things by taking one example.
Program 1:

    package scientecheasy; class School{ // Primitive Declaration. String name="RSVM"; // Instance variable public static void main(String[] args){ // Creating the 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 refernece variable sc Which is created above. System.out.println(sc.name); } }
    Output: School@1db9742 RSVM
From above example, you can understand 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. See below image.
Non primitive data type in Java with example,data types in java,How to declare non primitive data types in Java,what are the non primitive data types in java?, Difference between primitive and non primitive data type
Above image shows that 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.
So we can say that 'sc' variable does not contain the object. It refers to the object.


Types of Non-primitive Data types

1. Class
2. Object
3. String
4. Array
5. Interface
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 we can store multiple values either 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:
I hope this article would help you to understand Non-primitive data type in Java with examples. I always try to understand you in very simple ways so that you could not confuse. You read this article two or three times to understand the topic to clear the concepts.

Next ➝Memory allocation                                             "Learning Today For A Better Tomorrow."                    


Popular