• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

casting array types

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.
consider the following code:
class A {}
class B extends A {}
////some code here
/// in main()
int[] i = new int[10];
long[] l = new long[10];
// l = i; // this won't compile even though int can be assigned to long.
A[] a = new A[10];
B[] b = new B[10];
a = b; // this WILL compile because every element of a can be assigned to b
/////
I wanna make sure I understand this clearly. this happens because
array i and array l have primitive data types as elements. if elements of arrays are all object reference types and as long the elements of one array can be assigned to another, the whole array can be assigned to another array. is this correct? and this rule doesn't hold for array of primitive types.
thanks for the help! much appreciated.
chun
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your statement of the rule is exactly correct.
A primitive array reference can be assigned to an Object type variable and thats about it.
 
reply
    Bookmark Topic Watch Topic
  • New Topic