3

Possible Duplicate:
Why isn't there a java.lang.Array class? If a java array is an Object, shouldn't it extend Object?

I'd created an array say:

int a[] = new int[50];

Here, int is a primitive data type but in java, arrays are created at run-time which means that there would be an object of some class.
So, I would like to know which class object they are and what the background process happen while making array object?

And one more thing I would like to know, if I would like to print the reference id of that array:

System.out.println(a);

then the output is certain kind of [I@17182c1
Here I know that string before the @ is class name and after @ is the hashcode. But which class is [I?
How [I class is created and where from I got the variable length which return the length of the array? I had not found class [I in the whole API of Java, then why don't compiler shows error if I write:

System.out.println(a.length);

as the class [I will be created at run-time?

Community
  • 1
  • 1
Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117
  • 2
    Why is there no link to the original question here? I've seen it on other questions nominated for closure as duplicates. – David Harkness May 19 '12 at 21:06
  • I would like to know how exactly the class `[I` is created and where from the variable `length` I got? There is no class with name `[I` so how does it created and why does not the compiler shows error if I write `a.length;`? – Mohammad Faisal May 19 '12 at 21:07
  • 1
    `[I` is how Java represents `int[]` in the class's `toString` method. You can get the class using `int[].class`. See this [blog post](http://tutorials.jenkov.com/java-reflection/arrays.html#class) for some details. – David Harkness May 19 '12 at 21:14
  • @DavidHarkness: if I can't get class `[I` then how do I only get the variable `length` neither any other variable nor any method? – Mohammad Faisal May 19 '12 at 21:23
  • @DavidHarkness The duplicate that was used as a reason to close is http://stackoverflow.com/questions/8546500/why-isnt-there-a-java-lang-array-class-if-a-java-array-is-an-object-shouldnt you can see it in the edit history. The reason the link is missing is similar to this: http://meta.stackexchange.com/questions/30438/closing-as-exact-duplicate-missing-links-to-the-duplicate-questions – trutheality May 19 '12 at 22:38
  • @MohammadFaisal - It has a class that you can get as I showed in my comment. See the duplicate question linked to by trutheality for an explanation. – David Harkness May 20 '12 at 04:03
  • Why it is still closed as I don't get the answers to my core questions. Neither I get my answer on the duplicate link. – Mohammad Faisal May 20 '12 at 14:47
  • The println, for a non-string pointer, shows the class name `[I` followed by an identifier that is, IIRC, the hash value for the object (which is derived from the address in this case) -- only the first two characters are the class name. Array classes have names consisting of "[" followed by the obvious character to represent the primitive element type. Array classes are subclasses of Object with all the features of other classes, other than the `new` syntax. – Hot Licks May 20 '12 at 14:59
  • 1
    See http://stackoverflow.com/a/10674614/581994, where I address your issues to a degree. – Hot Licks May 20 '12 at 15:11
  • @HotLicks: still I don't get where from the variable `length` come from and why the compiler don't show error while using it, if the class is created by JVM as per your [answer](http://stackoverflow.com/a/10674614/581994)? Array has all the functionality of `Object` class as it extends it but what about `length`? – Mohammad Faisal May 21 '12 at 01:05
  • @MohammadFaisal -- The array classes, since they are "normal" classes, can have their own methods. Thus the array classes all define a `length` method. (It might make sense in a way if the array classes like `[I` and `[J` (which, oddly, is the "long" array class) were subclasses of `[`, and `[` in turn defined `length`, but that's not the way the designers of Java chose to do it.) – Hot Licks May 21 '12 at 02:16
  • @HotLicks: where could I find the `[` class in java-API? – Mohammad Faisal May 21 '12 at 15:25
  • @MohammadFaisal -- Like I said, that's not the way the designers of Java chose to do it. Rather, each individual `[x` class separately defines `length`, with no superclass other than Object. – Hot Licks May 21 '12 at 15:27
  • @MohammadFaisal -- And I should point out that I mispoke slightly above. `length` is not a *method* of array objects, but rather is a read-only *field* of array objects. – Hot Licks May 21 '12 at 15:34

0 Answers0