Non-Primitive Data Types in Java

PrimitiveNon-Primitive

In the previous blog, we learned about Data Types in Java. If you want to know more about it then visit Data Types in Java. In this blog, we will go through Non-Primitive Data types in Java. In Java Data Types are broadly divided into two categories:

  • Primitive Data types
  • Non-Primitive Data types

We have already learned about Primitive Data Types in our last blog. Now we will go through Non-Primitive Data Types.


Non-Primitive Data Types

Non-primitive data types are the data types that are created by programmers. They're not predefined in java like primitive data types. Non Primitive data types are used to store a group of values or several values. A variable of non-primitive data types references a memory location where data is stored within the heap memory. It references a memory where an object is really placed. Therefore non-primitive data types are called referenced data types. There are five non-primitive data types:


Class

class is created by a user(user-defined) in Java. The template or blueprint from which objects are made is called a class. When an object is created then it is said as an instance of the class. It represents the common attributes and functions of an object. class in Java is denoted by the keyword class.

e.g.

public class ClassEx {
  int no =10;
  char letter = 'S';
  float num = 10.9F;
  boolean flagValue = true;
  public void displayValues(){
    System.out.println("The value of int is:"+no);
    System.out.println("The value of char is:"+letter);
    System.out.println("The value of float is:"+num);
    System.out.println("The value of boolean is:"+flagValue);
  }
}

Object

Any real-world thing that has properties and actions is known as an object. It is an entity and it has behavior as well as state, where state represents properties and behavior represents actions or functionality.

e.g.

public static void main(String args[]) {
  ClassEx classObj = new ClassEx();
  classObj.displayValues();
}

Output

/* Output */
The value of int is:10
The value of char is:S
The value of float is:10.9
The value of boolean is:true

String

String in Java stores a sequence of characters. It is a non-primitive data type, but it is predefined in Java. As we know that in C and C++ it is mandatory to add the null character at the end of the string. But in Java it is not required, there is no need to add null at the end of the string.

e.g.

public class StringExample {
    String messageString = "Java Shortkicks";
    public void displayMessage(){
        System.out.println(messageString);
    }
    public static void main(String args[]) {
        DevStringExample obj  = new DevStringExample();
        obj.displayMessage();
    }
}

Output

/* Output */
Java Shortkicks

Array

Array in Java is used to store multiple variables of the same type. It is a non-primitive data type that stores multiple variables of the same type in a sequential manner. The declaration of an Array in Java is the same as we declare the normal variables in Java, but the difference is we have to add square brackets([ ]) along with the variable.

e.g.

public class ArrayEx {
    int []arrayObj = {40,50,60,70,80,90};
    //OR
    int arrayObj1[] = {40,50,60,70,80,90};
    public static void main(String args[]){
        ArrayEx obj  = new ArrayEx();
        System.out.println(obj.arrayObj[5]);
        System.out.println(obj.arrayObj1[5]);
    }
}

Output

/* Output */
90
90

Interface

Interface in Java is the same as a class that has methods and variables but the difference is that all the methods in an interface are abstract by default. We can only define the methods in the interface. The implementation of the method is done in its child class.

e.g.

interface InterfaceEx {
    public void displayMessage();
}
class ChildClass implements InterfaceEx {
    public void displayMessage(){
           System.out.println("Welcome to Java Shortkicks");
    }
    public static void main(String args[]) {
       ChildClass childObj = new ChildClass();
       childObj.displayMessage();
      }
}

Output

/* Output */
Welcome to Java Shortkicks

Core Java Tutorial

Get in Touch

Atrowel will be pleased to receive your feedback and suggestions. Those of you who would like to contribute, please send us an email.