46

Here is the java package-tree: http://docs.oracle.com/javase/7/docs/api/java/lang/package-tree.html

I read a tutorial on Java which stated that in Java arrays are objects.

Where is the array class? How comes we can make arrays like this:

byte[] byteArr = new byte[];
char[] charArr = new char[];
int[] intArr = new int[];

and the arrays will inherit methods from Object; for example:

    byte thisByte = 1;
    byte thatByte = 2;
    byte[] theseBytes = new byte[] {thisByte, thatByte};
    int inheritance = theseBytes.length; //inherited 'length' field and some methods
    int wasntInWill = thatByte.length; //error

What's going on here?

EDIT:

As per the answers, I now know it is a final class in java.lang.reflect package.

I have now created a package java.lang.reflect in my Android project and have added a class called Array.java to it. To confirm this is in the way of the original class, Eclipse gave me the error "... already exists in path/to/android.jar"

If I write out the same class as java.lang.reflect.Array but change the toString() method... this should work within my application right?

Afshin Moazami
  • 2,092
  • 5
  • 33
  • 55
Ozzy
  • 8,244
  • 7
  • 55
  • 95

4 Answers4

49

From JLS:

Every array has an associated Class object, shared with all other arrays with the same component type. [This] acts as if: the direct superclass of an array type is Object [and] every array type implements the interfaces Cloneable and java.io.Serializable.

This is shown by the following example code:

class Test {
    public static void main(String[] args) {
        int[] ia = new int[3];
        System.out.println(ia.getClass());
        System.out.println(ia.getClass().getSuperclass());
    }
}

which prints:

class [I
class java.lang.Object

where the string "[I" is the run-time type signature for the class object "array with component type int".

And yes, since array types effectively extend Object, you can invoke toString() on arrayObject also see the above example

int arr[] = new arr[2];
arr.toString();
Nils von Barth
  • 3,239
  • 2
  • 26
  • 27
jmj
  • 237,923
  • 42
  • 401
  • 438
  • Hi, thanks for the info. I editted my question to get to what I was really looking for - how to override the toString() method for arrays. – Ozzy Dec 17 '11 at 17:51
  • You are welcome, you don't have class in your hand to change, better create a util method that accepts array object and print it in the form you want – jmj Dec 17 '11 at 17:54
  • I know that I'm just trying to see if it can be done. Of course I should try it out before I ask that, but there was a little noob part of me that was worried it would blow up my computer! – Ozzy Dec 17 '11 at 17:56
  • 1
    For the toString() just use Arryas.asList() – Thorbjørn Ravn Andersen Dec 17 '11 at 18:08
  • I'm aware of this also, but thanks. I wanted to see if I can make it work for my arrays normally by writing the toString() method. – Ozzy Dec 17 '11 at 18:35
  • @JigarJoshi: I don't get how `[I` class is created and how did I get only the variable `length` for arrays? Also, why don't the compiler don't show error when I use `System.out.println(arr.length);` as array object is created at run-time? – Mohammad Faisal May 20 '12 at 14:28
10

Arrays are a language feature - they have a specific syntax for declaring and accessing. And their class definition is hidden from you.

They have a representation in the refleciton API - java.lang.reflect.Array

Btw, the length field is not inherited from Object. The .getClass(), .toString(), etc. methods are inherited.

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
  • Oh so java.lang.reflect.Array is the class definition? And yeah, I meant inherited from some array Object (to prove there is an array class somewhere) – Ozzy Dec 17 '11 at 17:30
  • 1
    `length` field doesn't exist, there is no such field in any array. It's just java language feature that represents `ARRAYLENGTH` opcode like field. Technically, virtually each impl. has such field in the object header, though. Arrays implement 2 interfaces as well: `Serializable` and `Clonable` and publish `clone()` method. – bestsss Dec 19 '11 at 10:07
  • 3
    @Ozzy No, `java.lang.reflect.Array` isn't the class definition. It is a utility reflection class that has nothing but static methods. – user207421 May 12 '14 at 08:17
  • 6
    @Bozho Any plans to fix this answer? You've had 4.5 years. – user207421 Jun 03 '16 at 10:24
  • 2
    Just to give latter comer more clarity. If you do `Integer[] array = new Integer[0]; System.out.println(array.getClass());`, you should see the class is `class [Ljava.lang.String;`, not `java.lang.reflect.Array` – somenickname Oct 18 '18 at 21:11
7

Slight elaboration of the above code segment:

public class ClassForName {
    public static void main(String[] argv) throws ClassNotFoundException {
        Class theClass = Class.forName("[I");
        System.out.println(theClass.getName());
        Class superClass = theClass.getSuperclass();
        System.out.println(superClass.getName());
    }
}

Results:

C:\JavaTools>java ClassForName
[I
java.lang.Object

As can be seen, "[I" is the name of the class which we would call, in English, "array of int". The class is a "full citizenship" Java class, which responds to all the methods of Object. The only difference is that the new syntax is different and it doesn't support the newInstance() method of Class.

(The classes "[I", "[C", et al, are "pre-defined" in the JVM -- there are no .class files corresponding to them. Java also will implicitly create, on the fly, the "[MyJavaClass;" class if you have an array of "MyJavaClass" in your program.)

Hot Licks
  • 47,103
  • 17
  • 93
  • 151
  • 1
    Mostly correct, but: 1. Further differences are that the class is both `Cloneable` and `Serializable`. 2. The [JLS](http://docs.oracle.com/javase/specs/jls/se8/html/jls-10.html#jls-10.8) and [JVM Specification](https://docs.oracle.com/javase/specs/jvms/se12/html/jvms-5.html#jvms-5.3.3) both state that *all* array classes are created dynamically. Nothing there about some of them being built-in. – user207421 May 09 '19 at 08:48
4

Why isn't there an array class?

There are array classes, one per element type used in the program. Both the JLS and the JVM Spec state clearly that a class object for an array is dynamically created. JLS #10.8 'Class Objects for Arrays' and JVM Spec #5.3.3 'Creating Array Classes'.

If a java array is an Object, shouldn't it extend Object?

It does extend java.lang.Object. JLS #10.8 states clearly that 'the direct superclass of every array type is Object.'

user207421
  • 305,947
  • 44
  • 307
  • 483
  • you're a bit late, I found the class already (see answer above) – Ozzy Dec 18 '11 at 11:09
  • @Ozzy You didn't find the class anywhere. It isn't defined anywhere. It isn't even a single class. There is one for each primitive type, e.g. `int[]`, and one for every object type, e.g. `Object[],` `String[],` etc. – user207421 May 12 '14 at 08:18
  • @user207421 in the JLS 10.8 it says that "an array type is not a class" . so is a dynamically created class, not a class? – csguy May 10 '19 at 04:12