Is an array a primitive type or an object (or something else entirely)?
Asked Answered
H

7

61

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...

Humpback answered 9/10, 2012 at 19:7 Comment(0)
E
71

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[].

Elenor answered 9/10, 2012 at 20:15 Comment(7)
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]?Humpback
the int[2] syntax was from C, which Java tried to be close to.Elenor
it's most likely that there's special code in the jvm to handle array initialization which is quite different than standard object instantiation.Grassquit
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?Humpback
that doesn't have to affect the choice of syntax.Elenor
note that the compile-time subtyping rule breaks type safety https://mcmap.net/q/324124/-subtypes-of-arraysMorbific
You did not answer if an array is an object or not.Wack
C
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.

Calotte answered 9/10, 2012 at 19:28 Comment(3)
Maybe I'm overthinking this... but how can something that isn't a class or a member of a class still extend Object?Humpback
@Jeff I can't really answer that. Maybe this helps. Moreover, there is an interesting answer here.Calotte
@Elenor do u mean int[].classChinn
N
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...

Nunuance answered 9/10, 2012 at 19:15 Comment(2)
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.Humpback
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..Nunuance
H
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.

Histo answered 9/10, 2012 at 19:15 Comment(8)
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... ?Humpback
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 upHisto
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?Humpback
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.Histo
I never said int[] doesn't inherit from Object. Please read again, it's subtle which is why you are confused.Histo
@Jeff There is some information here. Although the question aims at 2d arrays, the answers there should give you some insight into the mechanisms.Calotte
Ah, I see. I read your post too fast. And thanks for the link, Baz.Humpback
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 :)Histo
T
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

Tit answered 9/10, 2012 at 19:10 Comment(1)
I've looked at that documentation, but where is the API for it? That's what I can't find anywhere.Humpback
P
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.

Placket answered 9/10, 2012 at 19:49 Comment(0)
E
-1

It is an object

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

Ethbin answered 9/10, 2012 at 19:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.