58

The question is basically self-explanatory. I haven't been able to find an API for arrays (other than this Arrays, but this just defines a bunch of static helper functions for dealing with actual arrays). If there is no class for it, this seems to suggest that an array can't be an Object.

However, the fact that an array has public fields like length and methods that it can invoke like .equals() and .clone() seem to suggest (very strongly) the complete opposite.

What is the explanation for the odd presentation and behavior of primitive arrays?

As a note, I tried to use the "Open Implementation" Eclipse feature on the .clone() method of an array just now, hoping that I would be able to look at where and how this method was defined (since it said int[] overrode it from Object), but it actually caused my entire Eclipse to freeze up and crash...

asteri
  • 11,402
  • 13
  • 60
  • 84

7 Answers7

69

There is a class for every array type, so there's a class for int[], there's a class for Foo[]. These classes are created by JVM. You can access them by int[].class, Foo[].class. The direct super class of these classes are Object.class

public static void main(String[] args)
{
    test(int[].class);
    test(String[].class);
}

static void test(Class clazz)
{
    System.out.println(clazz.getName());
    System.out.println(clazz.getSuperclass());
    for(Class face : clazz.getInterfaces())
        System.out.println(face);
}

There's also a compile-time subtyping rule, if A is subtype of B, A[] is subtype of B[].

irreputable
  • 44,725
  • 9
  • 65
  • 93
  • 1
    Excellent answer! I understand now why I couldn't find any APIs or anything now. That's very interesting that the JVM makes these classes itself at runtime. One last question though: if an array is a member of a class just like any other object, how does initialization work? If what you're saying is accurate and [] is just part of the class name, why aren't they declared using a constructor, ie new int[](2) vs new int[2]? – asteri Oct 09 '12 at 20:49
  • the `int[2]` syntax was from C, which Java tried to be close to. – irreputable Oct 09 '12 at 20:53
  • 1
    it's most likely that there's special code in the jvm to handle array initialization which is quite different than standard object instantiation. – Matt Oct 09 '12 at 21:10
  • That's what I was thinking. So "under the hood", is the compiler extracting the number and passing it into the standard constructor that it made, or what? – asteri Oct 09 '12 at 21:18
  • that doesn't have to affect the choice of syntax. – irreputable Oct 09 '12 at 21:20
  • 1
    note that the compile-time subtyping rule breaks type safety http://stackoverflow.com/a/3712419/2558896 – adam.r Feb 27 '14 at 09:51
  • You did not answer if an array is an object or not. – Arun Raaj Aug 02 '18 at 19:54
28

The Java Language Specification should give you an idea:

The direct superclass of an array type is Object.

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

Moreover:

An object is a class instance or an array.

So arrays are not instances and therefore you don't need a constructor to create them. Instead you use the Array Creation Expressions.

Community
  • 1
  • 1
Baz
  • 36,440
  • 11
  • 68
  • 94
  • Maybe I'm overthinking this... but how can something that isn't a class or a member of a class still extend `Object`? – asteri Oct 09 '12 at 19:38
  • 1
    @Jeff I can't really answer that. Maybe [this](http://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html#jls-10.7) helps. Moreover, there is an interesting answer [here](http://stackoverflow.com/questions/2267790/how-are-arrays-implemented-in-java). – Baz Oct 09 '12 at 19:45
  • @irreputable do u mean int[].class – csguy May 05 '19 at 23:43
17

See the below code. It compiles:

    int[] arr = new int[2];
    System.out.println(arr.toString());

Now, on any primitive type, you cannot call a method(toString()) defined in Object class (Or, any method for that matter)... So, an array is essentially an Object.

OK, here you go:

From the JLS Section 4.3:

There are four kinds of reference types: class types (§8), interface types (§9), type variables (§4.4), and array types (§10).

And, Section 10:

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.

So, from the first quote, Array is not actually a class... It is another type. But, essentially arrays are objects, though not of some Class, but they are of Array type. So they are not instances of some class, and may be objects of array are defined to be created that way...

Community
  • 1
  • 1
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • I'm aware. I've used arrays before. The question just came to mind when realizing how differently arrays behave than actual, regular objects. For instance, in your "new" declaration, you don't use `new int[2]()`... because it's not a real constructor. Why? Where is the API? What is the explanation? It's less simple than you make it. – asteri Oct 09 '12 at 19:18
  • 1
    And I would prefer to know if someone doesn't like my answer, so that I can improve it, rather than just see the downvotes on it.. – Rohit Jain Oct 09 '12 at 19:31
7

So short and simple, yes <Type>[] is a type of Object. It extends directly from Object as I understand it. There are all the Object methods on it, toString(), hashCode(), ... Plus a special exposed variable called length. The class java.util.Arrays is a utility class for dealing with types of Arrays. It's a little confusing when you add to the mess things like: int[] does not inherit from Object[]. Also, unlike other Object types, there are no constructors for array types. They respect the new keyword but that is usually to allocate for the size. It's a little bizarre, but just one of those language quirks.

To answer the question though, yes they are an object.

Greg Giacovelli
  • 10,164
  • 2
  • 47
  • 64
  • An array is an object... but int[] does not inherit from Object? This confuses me. And I'm also confused by the absence of a constructor, since we use `new`... How does this work? Is a primitive array basically a cross between an Object and a hardwired primitive... ? – asteri Oct 09 '12 at 19:17
  • 1
    no int[] does not inherit from Object[]. Like int[] and Object[], String[] ... all different because they all inherit from Object, not Object[]. Generics help when writing methods that take arrays, as well as the Arrays utility class I mentioned, but yeah this weird quirk leads to some funky code sometimes just to get the types to line up – Greg Giacovelli Oct 09 '12 at 19:18
  • If int[] doesn't inherit from Object and int[] isn't a primitive, what is it? Is the main maxim of Java (that all objects extend `Object`) not wholly true, and there is a separate category of objects that arrays fall under? – asteri Oct 09 '12 at 19:20
  • 2
    Think of the `[]` as part of the Class name. So there is `int` which is primitive. There is `Integer` which is an `Object`. And there is `int[]` which is another type of `Object`. – Greg Giacovelli Oct 09 '12 at 19:21
  • I never said int[] doesn't inherit from Object. Please read again, it's subtle which is why you are confused. – Greg Giacovelli Oct 09 '12 at 19:22
  • 2
    @Jeff There is some information [here](http://stackoverflow.com/questions/2319724/multidimensional-arrays-in-java-extends-which-class). Although the question aims at 2d arrays, the answers there should give you some insight into the mechanisms. – Baz Oct 09 '12 at 19:23
  • Ah, I see. I read your post too fast. And thanks for the link, Baz. – asteri Oct 09 '12 at 19:31
  • Yeah sadly it's one of those things that makes total sense when you really sit down and think about, but it's a lot of times really not super convenient the way it work :( By the end of this little road block you will never forget this concept :) – Greg Giacovelli Oct 09 '12 at 19:39
3

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

See http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
  • 1
    I've looked at that documentation, but where is the API for it? That's what I can't find anywhere. – asteri Oct 09 '12 at 19:15
0

Only those several primitive types in Java as we known. Basically, we still have several steps to create an array, such as declare, construct or initialize if needed, and that means array is an object indeed.

Stepping deeper, the primitive types could be stored in memory with the original values but object is an address(reference). So we can imagine a paradox, how could we store the original values in the memory if the array is an primitive type? I think the same as String, but String is a final object so that you can construct an object in an easy way, String s = "s", like a primitive type.

Eric
  • 65
  • 1
-1

It is an object

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html

Daniel Pereira
  • 2,720
  • 2
  • 28
  • 40