106

In Java we can declare an array using the following:

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

Does this mean that the array itself is an object? I ask, because in C++ an array is just a pointer and does not have any methods.

d-cubed
  • 1,034
  • 5
  • 30
  • 58
Mike G
  • 4,829
  • 11
  • 47
  • 76
  • 5
    No, in C++ an array is an array, and not not an array. – Kerrek SB Jan 08 '12 at 20:33
  • 51
    `if (array instanceof Object) System.out.println("Yes!")` – skaffman Jan 08 '12 at 20:34
  • 6
    Oh, and in C++, both arrays and pointers are objects. – R. Martinho Fernandes Jan 08 '12 at 20:37
  • 2
    @R.MartinhoFernandes All C++ arrays are C++ objects indeed, but there are C++ pointers which are not C++ objects, namely all pointers which are the result of evaluating an rvalue. Examples include `&x`, `p+i` and `new int(42)`. – fredoverflow Jan 08 '12 at 21:48
  • 1
    @skaffman The message is not strictly necessary, since it wouldn't compile otherwise. – shmosel Jul 08 '16 at 20:41
  • I do not understand this comment " No, in C++ an array is an array, and not not an array. ". Can you explain it , please ? @Kerrek SB – Fady Hany May 29 '23 at 13:53
  • @R. Martinho Fernandes you said " Oh, and in C++, both arrays and pointers are objects. ". the textbook definition of Object is " object is an instance of a particular class or subclass with the class's own methods or procedures and data variables. ". and because of that I think in C++ pointer is a data type of a variable ( like int or float ) and in C++ array is a data structure ( So C++ pointers and arrays are not objects ) , if you totally agree with me delete your comment , please – Fady Hany May 29 '23 at 14:59
  • @R. Martinho Fernandes ??? – Fady Hany Aug 29 '23 at 11:52

14 Answers14

209

Yes.

The Java Language Specification section 4.3.1 starts off with:

An object is a class instance or an array.

Pang
  • 9,564
  • 146
  • 81
  • 122
Paul
  • 19,704
  • 14
  • 78
  • 96
  • 6
    Until now I always assumed an object was synonymous with class instance and that arrays were a special language feature or something. – Ruben9922 Aug 09 '17 at 12:40
  • Detailed explanation is given here:https://www.geeksforgeeks.org/array-primitive-type-object-java/ – garnet Jun 21 '18 at 13:40
64

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
meriton
  • 68,356
  • 14
  • 108
  • 175
  • 1
    If array is an object, doesn't that mean it is an instance of a class? If so, can I extend that class? – One Two Three Apr 03 '13 at 20:23
  • 12
    No, it doesn't: The Java spec [writes](http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.3.1): "An object is a class instance or an array. A class instance is explicitly created by a class instance creation expression (§15.9). An array is explicitly created by an array creation expression (§15.10). ". And no, you can not extend an array, because the extends clause [must](http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.1.4) feature a ClassType. – meriton Apr 03 '13 at 20:50
41

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
nasukkin
  • 2,460
  • 1
  • 12
  • 19
9

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.

Marsellus Wallace
  • 17,991
  • 25
  • 90
  • 154
5

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.

ed209
  • 828
  • 2
  • 14
  • 30
5
  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.

RFS
  • 311
  • 3
  • 8
3

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

naXa stands with Ukraine
  • 35,493
  • 19
  • 190
  • 259
2

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.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
1

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();

Manoj Mohanty
  • 372
  • 2
  • 10
1

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.

fastcodejava
  • 39,895
  • 28
  • 133
  • 186
1

In java, arrays are objects, and are dynamically created. Arrays can be assigned to variables of type Object, and all methods of type Object can be invoked on arrays. An array can contain multiple variables or also contain 0 variables in which case it is said to be empty.

Ayan
  • 39
  • 4
  • I did not get this part "all methods of type Object can be invoked on arrays". Could you please give an example of it? – codeogeek Jun 28 '21 at 04:27
  • 1
    @codeogeek A method is a block of code which only runs when it is called. You can pass data, known as parameters, into a method, they are used to perform certain actions. For example the toString() method converts an array into string representation. ``` Student[] arr = { new Student(111, "bbbb", "london"), new Student(131, "aaaa", "nyc"), new Student(121, "cccc", "jaipur") }; System.out.println(Arrays.toString(arr))``` Output: [111 bbbb london, 131 aaaa nyc, 121 cccc jaipur] – Ayan Jun 28 '21 at 12:52
0

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.

Vishnu Dahatonde
  • 179
  • 2
  • 13
0

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[].
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
0

Some suggested checking the value returned by instance of. I think that manner is semantically wrong because once you are able to write and compile such code:

    int[] myArray = new int[3];
    if (myArray instanceof Object) {
    }

It already proved that myArray is an object. If it were not, the code won't compile. As is written in a oracle tutorial

The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface

kukot
  • 1
  • 3