Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array Conversion

 
Satyashree Mahapatra
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Wondering why an int array int[] cannot be assigned to double[]
so if int[] i and double[] d are two arrays then d = i is not allowed. Whereas if A[] a and B[] b are arrays of objects where class B is a subclass of class A then a = b is valid.
Could anyone please throw some light on this ?
Thanks
Satya
 
Ajith Kallambella
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satya,
Your observations are right. Primitive array assignments do not undergo automatic widening conversions. Though an int can be assigned to a long, an int array cannot be assigned to a long array, even with explicit cast. Java simply cannot convert from array of one primitive type to another.
The only thing you can do, however, is assigning an int array to another int array. I know it is dumb, but they call it identity conversion. Rather a complicated term for a very trivial operation!!
Ajith
 
Satyashree Mahapatra
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ajith for your input.
Do you know as to why this feature was excluded. It would be great if you could give some pointers where I could get an answer.
Thanks
Satya
 
sath
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Satyashree ,
If you go thro RHE(page 118), it clearly states that:
When oth Oldtype and Newtype are arrays, both arrays MUST CONTAIN reference type (NOT PRIMITIVES), and it must be legal to casrt an element of Oldtype to an element of Newtype.

sath
 
Satyashree Mahapatra
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all for your help.
Regards
Satya
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic