wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array Conversion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array Conversion" Watch "Array Conversion" New topic
Author

Array Conversion

Satyashree Mahapatra
Greenhorn

Joined: Jul 18, 2000
Posts: 10
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

Joined: Mar 17, 2000
Posts: 5782
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


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Satyashree Mahapatra
Greenhorn

Joined: Jul 18, 2000
Posts: 10
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

Joined: Aug 24, 2000
Posts: 5
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

Joined: Jul 18, 2000
Posts: 10
Thank you all for your help.
Regards
Satya
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Array Conversion