Home » Non-primitive data types in Java

Non-primitive data types in Java

by Online Tutorials Library

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

  1. Primitive data types (predefined data types)
  2. Non-primitive 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.

Key points:

  1. The default value of any reference variable is null.
  2. Whenever we are passing a non-primitive data type to a method, we are passing the address of that object where the data is stored.

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

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.

Example:

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.

ClassExample.java

Output:

Addition of numbers is: 30  Subtraction of numbers is: 10  

2. Interface:

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.

Example:

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.

InterfaceExample.java

3. String:

A string represents a sequence of characters for example “Tutoraspire”, “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.

Example:

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.

StringExample.java

Output:

Hello! This is  

4. Array:

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.

Example:

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.

ArrayExample.java

Output:

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

  1. In Java, the primitive data types are system defined however we have to create and define the non-primitive data types.
  2. In primitive data type, variables can store only one value at a time. However in non-primitive data types, either multiple values of the same type or different type or both can be stored.
  3. All the data for primitive type variables are stored on the stack whereas, for reference types, the stack holds a pointer to the object on the heap.
  4. A primitive type starts with a lowercase letter, while non-primitive types start with an uppercase letter.
  5. The size of a primitive type depends on the data type, while non-primitive types have all the same size.

You may also like