• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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.
 
Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic