This week's giveaway is in the Spring forum.
We're giving away four copies of Microservices Testing (Live Project) and have Chris Love & Andres Sacco on-line!
See this thread for details.
Win a copy of Microservices Testing (Live Project) this week in the Spring 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:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Array assignment doubt

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How come this compiles

String[][] s=null;
Object[] o=s;

Even though it compiles why doesnt it generate runtime error as o and s are of different dimension.
 
author
Posts: 23925
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Satish Kota:
How come this compiles

String[][] s=null;
Object[] o=s;

Even though it compiles why doesnt it generate runtime error as o and s are of different dimension.



It compiles and runs fine because it is valid... basically...

"s" is an array of an array of strings. Since arrays are also objects, an "array of strings" is also an object. Hence, "s" is also an array of objects.

Henry
 
Satish Kota
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what makes it more confusing

class Animal { }
class Dog extends Animal { }

Dog[][] d=new Dog[9][];
Object o=d; //Compiles
Object[] o1=d; //Compiles
Object[][] o2=d; //Compiles, SAY THIS IS LINE 6
Object[][][] o3=d; //Doesnt Compile

Animal a=d; //Doesnt Compile
Animal[] a1=d; //Doesnt Compile
Animal[][] a2=d; // Compiles
Animal[][][] a3=d; //Doesnt Compile


Dog[] d=new Dog[9];
Object o=d; //Compiles
Object[] o1=d; //Compiles

Animal a1=d; //Doesnt Compile
Animal[] a2=d; //Compiles

int[][] i=new int[9][]; //Compiles
Object o=i; //Compiles
Object[] o1=i; //Compiles
Object[][] o2=i; // Doesnt Compile, SAY THIS IS LINE 22

int[] i=new int[9]; //Compiles
Object o=i; //Compiles
Object[] o1=i; // Doesnt Compile, SAY THIS IS LINE 26


When Line 6 compiles why are Line 22,26 generating error. In both cases the indexes are same. Then why is this error coming ?
[ November 27, 2006: Message edited by: Satish Kota ]
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You've misread/misinterpretted Henry's response.

It's not that the type of object you're storing in your array is of type Object, it's the fact that arrays themselves are objects.

Therefore:
* A String array can be assigned to an object
* An array of String arrays (and using the principle above) can be assigned to an array of objects (your String arrays are an array of objects).

If you understand that then you'll understand why you can't assign an array of String arrays to a 3d object array - or anything else with that many dimensions.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic