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

JavaArraysObjectPrimitive

Java Problem Overview


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

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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.

Solution 3 - Java

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

Solution 4 - Java

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.

Solution 5 - Java

> 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

Solution 6 - Java

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.

Solution 7 - Java

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionasteriView Question on Stackoverflow
Solution 1 - JavairreputableView Answer on Stackoverflow
Solution 2 - JavaBazView Answer on Stackoverflow
Solution 3 - JavaRohit JainView Answer on Stackoverflow
Solution 4 - JavaGreg GiacovelliView Answer on Stackoverflow
Solution 5 - JavaAleksandr MView Answer on Stackoverflow
Solution 6 - JavaEricView Answer on Stackoverflow
Solution 7 - JavaDaniel PereiraView Answer on Stackoverflow