• 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

array doubt

 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int[] a = null , b [] = null;
b = a;
System.out.println( b );
output is compiler error. Explaination is " because b is a two dimensional array & a is one dimensoinal array, you cannot assign one dim. array to two dim. array.

How b is a two dimensional array?
 
Ranch Hand
Posts: 229
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess maybe that's not appropriate way to delare arrays...

int[] a = null , b [] = null;

I believe it gets translated to:

int[] a = null;
int[][] b = null;

Or you can do:
int[] a , b;
b = a = null;
System.out.println( b );
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After declaring a type, any subsequent identifiers on that line can add array dimensions, provided that the brackets indicating the additional dimensions come after the associated identifier. For example...

int[][]a, b, c[];

In the above declaration, a and b are both declared as 2-dimensional arrays, while c adds a dimension for a 3-dimensional array.

Now consider this...

int d[][], e;

Here, d is a 2-dimensional array (an object), while e is a primitive int.
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks marc. I did not know this concept. I think I should not expect this kind of questions on the real exam as it is not mentioned in the k & b book. Or should I? Let me know.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it is not mentioned in K&B. I saw this here on the ranch for the first time. An older thread, don't ask me when.

Bu.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic