For example, boolean, char, byte, short, int, long, float, and double. These eight data types are called primitive data types.
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.
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.
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”.
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:
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.
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 and objects: Every class is data type and it is also considered as user-defined data types. This is because a user creates a class. For more details: Class and objects in java.
2. String: A string represents a sequence of characters like India, ABC123, etc. The simplest way to create a string object is by storing sequence of characters into string type variable like this:
Here, string type variable str contains “Universe”. A string is also a class. For more details: String in Java.
3. Array: An array in java is an object which is used to store multiple variables of the same type. These variables can be primitive or non-primitive data type.
The example of declaring an array variable of primitive data type int is as follows:
int [ ] scores;
The example of declaring an array variable of non-primitive data type is
Student [ ] students; // Student is a name of class.
You will learn more details in further tutorials.
4. Interface: An interface is declared like a class but the only difference is that it contains only final variables and method declarations. It is fully abstract class.
Here, we have given just basic knowledge of non-primitive data types in java. You will get more knowledge in further 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.
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!