Is an array an object in Java?

前端 未结 12 1759
我寻月下人不归
我寻月下人不归 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:38

    Yes; the Java Language Specification writes:

    In the Java programming language, arrays are objects (§4.3.1), are dynamically created, and may be assigned to variables of type Object (§4.3.2). All methods of class Object may be invoked on an array.

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

    I just want to add that arrays have a representation in the reflection API - java.lang.reflect.Array.

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

    Yes, it is an object in Java.

    Also note that when you do array.length you're not invoking any methods but just accessing the array's length field. There are plenty of static methods in the Arrays class.

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

    I would say the answer is yes, although I might add that, strictly speaking, an array is an object in C++ too. From §1.8 [intro.object] of the current standard (FDIS):

    An object is a region of storage.

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

    Yes.

    The Java Language Specification section 4.3.1 starts off with:

    An object is a class instance or an array.

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

    Every array in java is an object ex int[] a=new int [2]; So new is used to create an object and as it is an object we can check the class name just using a.getClass().getName();

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