Ad Code

Non-Primitive Data Types in Java Part-2

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.

1)Class

2)Object

3)String

4)Array

5)Interface


1)Class

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.

public class ClassExample {  
        // defining the variables of class  
        int a = 20;  
        int b = 10;  
        int c;  
        // defining the methods of class  
        public void add () {  
            int c = a + b;  
            System.out.println("Addition of numbers is: " + c);  
}  
        public void sub () {  
            int c = a - b;  
            System.out.println("Subtraction of numbers is: " + c);  
        }  
    // main method  
    public static void main (String[] args) {  
        // creating the object of class  
        ClassExample obj = new ClassExample();  
        // calling the methods  
        obj.add();  
        obj.sub();  
    }  
}  

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.

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.

interface CalcInterface {  
    void multiply();  
    void divide();  
}  
public class InterfaceExample implements CalcInterface {  
        // defining the variables of class  
        int a = 10;  
        int b = 20;  
        int c;  
        // implementing the interface methods  
        public void multiply() {  
            int c = a * b;  
            System.out.println("Multiplication of numbers is: " + c);  
 }  
        public void divide() {  
            int c = a / b;  
            System.out.println("Division of numbers is: " + c);  
        }  
    // main method  
    public static void main (String[] args) throws IOException {  
        InterfaceExample obj = new InterfaceExample();  
        // calling the methods  
        obj.multiply();  
        obj.divide();  
    }  

3. String:

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:

}  

String str = "You're the best";  

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.

public class StringExample {  
    public static void main(String[] args) {  
        // creating a string and initializing it  
        String str = "Hello! This is example of String type";  
        // applying substring() on above string  
        String subStr = str.substring(0,14);  
        // printing the string  
        System.out.println(subStr);  
    }  
}  

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:

int [ ] marks;  

Following example shows how to declare array of non-primitive data type:

Student [ ] students;   

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.

// importing required packages  
import java.io. * ;  
import java.util. * ;    
public class ArrayExample {  
  public static void main(String[] args) throws IOException {  
    int i;  
    Scanner sc = new Scanner(System. in );  
    // declaring and initializing an array  
    int arr[] = {1, 2, 3, 6, 9};  
    // defining another array arr1  
    int arr1[] = new int[5];  
    // reading values from the user  
    System.out.println("Enter the numbers (size = 5) :");  
    for (i = 0; i < 5; i++) {  
      arr1[i] = sc.nextInt();  
    }  
    System.out.println("Previous array with initialized size is: ");  
    for (i = 0; i < 5; i++) {  
      System.out.print(arr[i] + " ");  
    }  
    System.out.println("\nThe new array we have entered is:");  
    for (i = 0; i < 5; i++) {  
      System.out.print(arr1[i] + " ");  
    }  
  }  
}  

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.

Ad Code

Responsive Advertisement