| Author |
Casting of int[] to Object[]
|
Sri Amb
Greenhorn
Joined: Nov 03, 2009
Posts: 2
|
|
When i do something like
it works because of autoboxing
But why didn't the language designers support something like this ?
Why didn't they support something like this. Went through JLS but couldnt find what i wanted.
Thanks in advance for your time.
|
 |
Mark Uppeteer
Ranch Hand
Joined: Mar 02, 2004
Posts: 159
|
|
Why not use an Integer[] and pass that to the method ?
its not autoboxing but it will work.
|
I know where my towel is. (SCJP 5, OCPJWCD)
[Free Quiz Tips for a fun night with friends or family] Flash games
|
 |
Adam Michalik
Ranch Hand
Joined: Feb 18, 2008
Posts: 128
|
|
That would be technically impossible. If you create an array as int[], the ints are stored directly in the array. If you create it as Object[] or Integer[], the references are stored in the array and the object elsewhere (on the heap). So suppose you had:
Now, the compiler, when it compiles class B does not know that the objects array is full of ints and threats it as if it was full of references. So when you try to access objects[1], it generates instructions to access an object via a reference. Auto(un)boxing happens at compile time, so there is no possibility to make an object from that int during runtime.
|
 |
Sri Amb
Greenhorn
Joined: Nov 03, 2009
Posts: 2
|
|
|
That makes sense. Thank you Adam..
|
 |
 |
|
|
subject: Casting of int[] to Object[]
|
|
|