• 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

cloning

 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All..
check this out..
int a[][] = {{1,2}, null};
int b[][] = (int[][])a.clone();
a[0] == b[0] //Equal
a[0][1] == b[0][1] //Equal but sub-arrays are shared
a == b //Not Equal
Now..
int ai][] = {{1,2}, null};
long bi][] = (long[][])ai.clone();//Compiles!!! But ClassCast
My understanding was
Arrays are objects in Java and they cant be casted implicitly/explcitily as the compiler will catch it
But how come in the second case it compiles???
Ragu
 
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
I guess it compiles because the compiler does not make any assumptions about what the ai.clone method call does.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Compiles because clone returns Object. Only at runtime the JVM finds out the cast is not possible.
Please read JLS 5.5
 
reply
    Bookmark Topic Watch Topic
  • New Topic