Non-Primitive Data Types in Java

Hello readers, In today’s topic we will discuss non-primitive data types in java, before that we also discussed primitive data types in java which are different types of, if you don’t know definitely watch that: Data Types In JAVA. Anyways now without wasting any time let’s dive into the topic:

Data types are basically used for declaring variables and functions of different types. It can define different sizes and values which are used to store variables.

In Java, data types are two types i.e.:

Non-Primitive Data Types

Non-primitive data types are also called reference types. This is because these data types are directly referred to as objects. It means like primitive data types these data types are not pre-defined. These are popular for user-defined which are created by programmers. There are various types of non-primitive data types in java i.e.:

  • Class
  • Object
  • String
  • Array
  • Interface

Class

In Java, a class is a data type that is defined by the user it means it is a prototype through which objects are created. It performs like a frame or template or you can say that it is a set of properties that consists of variables and methods. Generally, declaration of class can include various components i.e.: Modifier, Class Name, supper class(if required), interface (if required), and class body.

Example:-

We have to use the ‘class’ keyword for declaring a class. The first letter of a class name should always start with an uppercase letter and we have to also check the name of the Java file name should match the class name.

public class oraltadalafil {
    public void add (int num_1, int num_2) {
        int c = num_1 + num_2;
        System.out.println("Addition of "+num_1+" & "+num_2+" is: " + c);
    }
    public void sub (int num_1, int num_2) {
        int c = num_1 - num_2;
        System.out.println("Subtraction of "+num_2+" from "+num_1+" is: " + c);
    }
    public static void main(String[] args) {
        oraltadaafil GTC = new oraltadalafil();
        GTC.add(10,5);
        GTC.sub(20,15);
    }
}

OUTPUT:

Addition of 10 & 5 is: 15
Subtraction of 15 from 20 is: 5

Object

In Java, the object is a runtime entity. Generally, we can say that object is an instance of a class. It is created from a particular class, In the above example we have already created a class named GeektoCode, so now we can use this to create objects. And here the object is a variable of a class, which can be used to access every element of the class.

Example:-

public class oraltadalafil {
    public void add (int num_1,int num_2) {
        int c = num_1 + num_2;
        System.out.println("Addition of "+num_1+" & "+num_2+" is: " + c);
    }
    public void sub (int num_1, int num_2) {
        int c = num_1 - num_2;
        System.out.println("Subtraction of "+num_2+" from "+num_1+" is: " + c);
    }
    public static void main(String[] args) {
        oraltadalafil GTC = new oraltadalafil();
        GTC.add(10,5);
        GTC.sub(20,15);
    }
}

OUTPUT:-

Addition of 10 & 5 is: 15 
Subtraction of 15 from 20 is: 5

Here in the above example within the main method, ‘GTC’ is the object of GeektoCode class which is declared by using the ‘new’ keyword.

String

Generally, a string represents the sequence of characters. It can be defined as an array of characters that are stored sequentially or you can say that string can be designed to hold a sequence of characters in a single variable. The string object can be created by using java.lang.String class.

Example:-

A string variable can also be declared like other variables but its first latter can be declared as Uppercase.

public class oraltadalafil {
    public static void main(String[] args) {
        String gtc = "Hello!! Welcome to the Java Tutorials of oraltadalafil";
        System.out.println(gtc);
    }
}

OUTPUT:-

Hello!! Welcome to the Java Tutorials of oraltadalafil

Array

An array is a unique data type that is used to store a group of homogeneous/similar types of data in a sequence manner. It can be stored data according to its index value that is starting from 0. So the elements of an array can be accessed by their index number.

Example:-

The variable of an array can be declared like other variables with [] after the data type.

public class oraltadalafil {
    public static void main(String[] args) {
        int[] gtc = {1,2,3,6,8,4,9,10};
        for(int i =0;i<gtc.length;i++){
            System.out.println("The element of index["+i+"] is "+gtc[i]);
        }
    }
}

OUTPUT:-

The element of index[0] is 1
The element of index[1] is 2
The element of index[2] is 3
The element of index[3] is 6
The element of index[4] is 8
The element of index[5] is 4
The element of index[6] is 9
The element of index[7] is 10

Here for accessing the elements of an array we have to use for loop for every single variable of an array.

Interface

The interface is also likely similar to a class here also we declare methods and variables. But here the methods are by default in abstract type, which means those methods haven’t any body. It is also a blueprint of a class. Here the methods with no body are used to declare in another class by implementing the interface. We can also say to the interface is a fully abstract class.

Example:-

The interface can be declared by using the ‘Interface’ keyword.

interface JavaInterface{
 void sum(int a, int b);
 void sub(int a, int b);
}
public class oraltadalafi implements JavaInterface{
    @Override
    public void sum(int a, int b) {
        int c = a+b;
        System.out.println("Addition of "+a+" & "+b+" is: " + c);
    }
    @Override
    public void sub(int a, int b) {
        int c = a-b;
        System.out.println("Subtraction of "+b+" from "+a+" is: " + c);
    }
    public static void main(String[] args) {
        oraltadalafi GTC = new oraltadalafi();
        GTC.sum(10,5);
        GTC.sub(20,15);
    }
}

OUTPUT:-

Addition of 10 & 5 is: 15
Subtraction of 15 from 20 is: 5

So this is all about the Data types in Java. These are the various terms and concepts which are used in a java program and are very important concepts. I hope this is helpful to you. In the next upcoming post we will discuss another new topic of java, so wait for this will meet in the next post. Thank you.

For more read with us Oraltadalafil

Read our previous post

Leave a Comment