aspose file tools
The moose likes Java in General and the fly likes Casting of int[] to Object[] Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Casting of int[] to Object[]" Watch "Casting of int[] to Object[]" New topic
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..
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Casting of int[] to Object[]
 
Similar Threads
Constructor
Arrays
stars
Literal array in Object[] initialization
var args method