Is an array an object in Java?

前端 未结 12 1753
我寻月下人不归
我寻月下人不归 2020-11-22 14:26

In Java we can declare an array using the following:

String[] array = new String[10]; 
int size = array.length; 

Does this mean that the arra

相关标签:
12条回答
  • 2020-11-22 14:54

    Observe below code snippet and output.

    public class Tester {
    int a[];
    public static void main(String[] args) {
        System.out.println(new Tester().a);// null
        System.out.println(new Tester().a[0]);// Exception in thread "main" java.lang.NullPointerException \n at mainclass.Tester.main(Tester.java:10)
    }
    

    }

    clearly array a is treated as object.

    0 讨论(0)
  • 2020-11-22 14:55

    It would be important to note that arrays in Java have their own byte codes which they do not share with objects. They are certainly Objects, but are handled slightly differently at the low level.

    0 讨论(0)
  • 2020-11-22 14:57

    Yes, the docs say so:

    An array is a container object that holds a fixed number of values of a single type.

    Note that array types of primitive types (like int[] or char[]) themselves are also objects.

    Every array directly extends java.lang.Object and implements the interfaces javs.lang.Cloneable and java.io.Serializable. The runtime type signature of an array is [L immediately followed class name of component type (e.g. [Ljava.lang.String). Arrays of primitive types have the following runtime signature:

    • [B for byte[];
    • [S for short[];
    • [I for int[];
    • [J for long[];
    • [F for float[];
    • [D for double[];
    • [C for char[].
    0 讨论(0)
  • 2020-11-22 15:01

    Well, let's ask Java!

    public class HelloWorld
    {
      public static void main(String[] args)
      {
        System.out.println(args instanceof Object);
        int[] someIntegers = new int[] {42};
        System.out.println(someIntegers instanceof Object);
      }
    }
    

    Output:

    true
    true
    
    0 讨论(0)
  • 2020-11-22 15:01
    1. An array is not an instance of any class listed in the class tree, however each array is an object and inherits directly from java.util.Object
    (new int[1]) instanceof Object   // -> evaluates to true
    
    1. The class java.util.Arrays is a helper class, and arrays are not instances of this class.
    (new int[1]) instanceof java.util.Arrays    // -> compile error
    
    1. The class java.lang.reflect.Array is a helper class, and arrays are not instances of this class.
    (new int[1]) instanceof java.lang.reflect.Array    // -> compile error
    
    1. Arrays inherit all the members of java.lang.Object

    2. Arrays override the method clone() inherited from Object.

    3. Arrays implement the field length, which contains the number of components of the array. length may be positive or zero. It is public and final.

    4. Arrays implement the interfaces Cloneable and java.io.Serializable.

    8a. Arrays are supported by Class<T>. You can retrieve the Class<T> instance from an array instance

    (new int[2]).getClass()

    or from an array type

    int[].class

    8b. A unique reflection class instance (ie an instance of java.lang.Class<T>) is created for each different array type in your code. Examples

    int[].class.getCanonicalName()    //  -> "int[]"
    String[].class.getCanonicalName() //  -> "java.lang.String[]" /
    
    1. To repeat: Arrays are objects but are not instances of any class in the class tree.

    REFERENCES

    From the Java specification Section 4.3.1 Objects

    • An object is a class instance or an array.

    • A class instance is explicitly created by a class instance creation expression.

    • An array is explicitly created by an array creation expression.

    From java.util.Arrays

    • This class contains various methods for manipulating arrays (such as sorting and searching)

    From java.lang.reflect.Array

    • The Array class provides static methods to dynamically create and access Java arrays.

    From Section 10.1 Objects

    • The direct superclass of an array type is Object.

    • Every array type implements the interfaces Cloneable and java.io.Serializable.

    From Section 10.7 Array Members

    The members of an array type are all of the following:

    • The public final field length, which contains the number of components of the array. length may be positive or zero.

    • The public method clone, which overrides the method of the same name in class Object and throws no checked exceptions. The return type of the clone method of an array type T[] is T[].

    • A clone of a multidimensional array is shallow, which is to say that it creates only a single new array. Subarrays are shared.

    • All the members inherited from class Object; the only method of Object that is not inherited is its clone method.

    0 讨论(0)
  • 2020-11-22 15:01

    Arrays of anything are objects. One can call methods such as equals, hashcode etc:

    final int[] i = {};
    i.equals(new int[] {1});  // false
    i.hashcode();
    

    One cannot call methods on a native type.

    0 讨论(0)
提交回复
热议问题