Win a copy of Head First Android this week in the Android 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
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Array Conversion

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for your help.
Regards
Satya
reply
    Bookmark Topic Watch Topic
  • New Topic