Non-primitive data types in Java
Data types define the type of data that is stored in the variable. The type specifies the kind of data (different sizes and values).
Java programming language has two types of data types
In this section, we will understand the non-primitive data types, their uses and implementation in Java.
Non-primitive data types
Unlike primitive data types, these are not predefined. These are user-defined data types created by programmers. These data types are used to store multiple values.
For example, consider an array that stores a group of values. Class is also a primitive type that stores different methods and variables. Therefore, these are also called as advanced data types in Java.
Whenever a non-primitive data type is defined, it refers a memory location where the data is stored in heap memory i.e., it refers to the memory location where an object is placed. Therefore, a non-primitive data type variable is also called referenced data type or simply object reference variable.
An 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 that are created by instantiating a class.
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:
A class in Java is a user defined data type i.e. it is created by the user. It acts a template to the data which consists of member variables and methods.
An object is the variable of the class, which can access the elements of class i.e. methods and variables.
In the following example, we are creating a class containing the variables and methods ( add() and sub() ). Here, we are accessing the methods using the object of the Class obj.
Addition of numbers is: 30 Subtraction of numbers is: 10
An interface is similar to a class however the only difference is that its methods are abstract by default i.e. they do not have body. An interface has only the final variables and method declarations. It is also called a fully abstract class.
Note: If the class implements an interface, it must implement all the methods of that interface. If not, we must declare the class as abstract.
In the following example, we are creating the interface CalcInterface with two abstract methods ( multiply() and divide() ). Here, the class InterfaceExample implements the interface and further defines the methods of that interface. Then, the object of class is used to access those methods.
A string represents a sequence of characters for example "Javatpoint", "Hello world", etc. String is the class of Java.
One of the ways to create a string and store a value in it is shown below:
Here, String type variable str has the value "You're the best". Click here to understand more about String in Java.
In the following example, we are creating a string with a value. Here, we are using one of the String class methods, substring() which prints the specified indexed part of the string.
Hello! This is
An array is a data type which can store multiple homogenous variables i.e., variables of same type in a sequence. They are stored in an indexed manner starting with index 0. The variables can be either primitive or non-primitive data types.
Following example shows how to declare array of primitive data type int:
Following example shows how to declare array of non-primitive data type:
where, Student is the class name and [ ] creates an array of object students.
In the following example, we are creating two basic array, in which one is initialized and the other is declared (input is read from the user). Further, we are printing those array using the for loop.
Enter the numbers (size = 5) : 56 43 22 1 7 Previous array with initialized size is: 1 2 3 6 9 The new array we have entered is: 56 43 22 1 7
Difference between Primitive and Non-primitive Data types in Java
Next TopicPancake Number in Java