Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Casting of int[] to Object[]

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 159
Eclipse IDE C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why not use an Integer[] and pass that to the method ?
its not autoboxing but it will work.
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That makes sense. Thank you Adam..
 
reply
    Bookmark Topic Watch Topic
  • New Topic