• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

int array and object array

 
prateek shaw
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All ,

Most of time i get confuse about this topic.I know that array is object and it can hold the primitive data type and as well as object. However  ArrayList only hold Object.
Since array can hold primitive data type then some assignment became confusing.  

For me some time i get confuse or forget if i get some question which is based on this topic.The way i understand is below.


int[] mean that array of int , and Object[] mean that array of Object.However below code will work

In above i am creating array of Integer type and assigning to array of object type.
The point here to remember that the autoboxing and unboxing only happened to primitive type to their respective wrapper type. Not array of int to array of integer.

Let see below.


In above sample example, i can able to assign primitive type to wrapper class , primitive type to object. compile is doing all work here, converting primitive type to object and then converting object to primitive type.
The primitive type variable hold value directly however object type reference variable hole address where object is store.

Is my understanding is correct ? Or is something is more to understand ?





 
prateek shaw
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And also i would like to ask , if java can support autoboxing and unboxing for primitive type and wrapper class then why they can not support these for array of int and array of Integer.

 
Henry Wong
author
Marshal
Pie
Posts: 22094
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
prateek shaw wrote:And also i would like to ask , if java can support autoboxing and unboxing for primitive type and wrapper class then why they can not support these for array of int and array of Integer.


The Java Language Specification does not define any automatic conversion between array types of primitives and array types of their wrapper counterparts... perhaps, it may do so in the future, but as of the latest version of Java, there is no such support.

Henry
 
John Pacuta
Greenhorn
Posts: 26
1
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prateek,

I stepped through your code and renamed some variables. It seems like the underlying question is: How do I cast an Integer into the primitive type int?



For more information about unboxing look here: The Collection Framework
 
John Pacuta
Greenhorn
Posts: 26
1
Android Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prateek,

So how exactly would one successfully convert an array of Integers to an array of primitive ints?

 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic