5

Possible Duplicate:
How are arrays implemented in java?
Why isn’t there a java.lang.Array class? If a java array is an Object, shouldn’t it extend Object?

I was recently thinking about Java arrays and I was wondering what exactly are they?

Are they objects?

  • You can call toString() on them (even though it only gives you the
    memory address)
  • The following line compiles Object a = new int[5];

However, are they really objects?

  • You create a new array by new int[5]...this is like no constructor I've ever seen. One would think it would be new Array<Integer>(5) or something of the sort
  • If you look through the Java API you won't find an array class...You'll find Array but this isn't truely an array

And what's up with it's iterable properties

  • You can go through an array in a for each loop, but there is no iterator method of array that I can find

I think array is just some special exception built directly into the Java language; however, this also poses the question: why are arrays given special treatment? (Why isn't there an Array API? Why is there a different type of constructor?)

Any clarification regarding the true nature of arrays would be appreciated.

P.S. I have a few years experience in Java, so I know how to use arrays and what they do, I'm just asking how exactly they are implemented and why?

Community
  • 1
  • 1
Nosrettap
  • 10,940
  • 23
  • 85
  • 140
  • 2
    strikes me as a function of Java development early on-- I've heard that a lot of the functions/objects we know and love were basically developed by interns as summer projects. Which is why we have `.length`, `.length()` and `.size()` – Colleen Jan 11 '13 at 21:37
  • 2
    @colleen - got any proof? – radai Jan 11 '13 at 21:39
  • None at all. Which is why I said "I've heard". That was just something my Software Engineering teacher said. – Colleen Jan 11 '13 at 21:39
  • They're object-like. int[n] is syntatic sugar, like `String s = "ohai";` AFAIK it was implemented like that so it looked like C/C++, and a lot of that stuff was implemented before Java was standardized (e.g., Java APIs don't follow JavaBean conventions, camelCasing wasn't always consistent, etc.) – Dave Newton Jan 11 '13 at 21:41
  • Responding to the third part: arrays don't implement `Iterable` - the for each syntax on arrays only looks the same but is really just a separate language feature. – Paul Bellora Jan 11 '13 at 21:45
  • 1
    @DaveNewton They are objects. JLS chapter 10. int[n] is syntax, and has nothing to do with Strings. – user207421 Jan 11 '13 at 23:30
  • @EJP `int[n]` is syntax, so is `"foo"`: String literals are syntactic sugar that create string objects. IMO arrays behave as chimeras, half-way between primitives and objects. See the c2wiki page for a long-winded discussion. – Dave Newton Jan 11 '13 at 23:46
  • @DaveNewton I agree that String literals are indeed syntactic sugar, and I didn't say otherwise. I disagree that `int[n]` is, and you now seem to be agreeing with me. Re objects, your opinion is your opinion but what it says in the JLS #10 is fact. I don't know what c2wiki is, or where it is, but I am a compiler writer, and I'm not much interested in third party discussions unless they are informed. – user207421 Jan 12 '13 at 00:49
  • @EJP Not sure what being a compiler writer has to do with anything. I'd venture that in general, c2wiki participants are among the better-informed. The JLS can say whatever it wants-and I agree that arrays are objects-butt they do not *behave* as other objects in the JVM, and as a compiler writer, that should be important to you. YMMV. – Dave Newton Jan 12 '13 at 01:34

0 Answers0