Is an array a primitive type or an object in Java?

JavaObject Oriented ProgrammingProgramming

Array is considered to be an object in Java. The reason behind this is that an array can be created using the ‘new’ keyword. The ‘new’ keyword/operator is always used to create an object. This is how an array is perceived as an object.

The direct parent class or super class of any array is the ‘Object’ class. Every array type in Java belongs to a certain class. This indicates that there are explicit classes for integer array types, float array types, double array types, and so on.

Arrays can be dynamically created, and be assigned variables as well.

Let us see an example −

Example

 Live Demo

public class Demo{
   public static void main(String[] args){
      System.out.println("Is the argument an instance of super class Object? ");
      System.out.println(args instanceof Object);
      int[] my_arr = new int[4];
      System.out.println("Is the array my_arr an instance of super class Object? ");
      System.out.println(my_arr instanceof Object);
   }
}

Output

Is the argument an instance of super class Object?
true
Is the array my_arr an instance of super class Object?
true

A class named Demo contains the main function and the ‘instanceof’ operator is used to check if ‘args’ is an instance of the Object and if the newly created array is an instance of the Object. The results are displayed on the console.

raja
Published on 04-Jul-2020 08:44:38
Advertisements