Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confusing array declarations (was:A Good One)

 
Praveen Babu
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,



Can you guess the dimension of array2 ?

Answer : 3

not convinced ? try this

array1 = array2;

and the compiler error you get is

incompatible types
found : int[][][]
required: int[][]
array1 = array2;
^

Thought i would share this with you all.

Regards,
Praveen
[ September 02, 2006: Message edited by: Barry Gaunt ]
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I liked the question of Praveen. And it inspired me to this exam question.

Given:


What happens when you compile and execute this code?
(JSE 1.5)

Choose all that apply!

ACompilation fails due to an error in line 1
BCompilation fails due to an error in line 2
CCompilation fails due to an error in line 3
DCompilation fails due to an error in line 4
ECompilation fails due to an error in line 5
FCompiles and throws an Exception when executed
GCompiles and prints trueREADY
HCompiles and prints falseREADY
ICompiles and prints nullREADY
JVM dependent
KNone of the above



Hope you enjoy it
;-)
Bu.


pm: Hint: Kombination A,K is wrong
 
Praveen Babu
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its better than mine.
 
bing marquez
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
consider:

int [] array;
int array [];

both declarations are the same in that they declare an array of ints (1D)
even if the brackets were positioned differently (though the first declarion is more readable & recommended). however, position of the brackets become significant when multiple declaration is involved:

int [] array1 [] , array2 , array3 [][];

such that

array1 -> 2D array
array2 -> 1D array
array3 -> 3D array

the bracket near the type applies to the type and all variables included in thedeclaration. while the bracket near the variable applies only to that variable.

hope this provides a helpful explanation regarding this issue on array brackets...
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
praveen babu recently wrote
Its better than mine.

Different, but not better.
Bu.
 
Praveen Babu
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Thanks Bing for your explanation.Also thanks to Hassel.How about dedicating this post to the deadly traps that we come across so that it serves as a one stop solution to all the ranchers.

So if its leagal, please post more traps here.

Thanks & Regards,
Praveen
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will find this topic written about in the K & B book (or any good Java book) together with the comment to the effect of "it may be legal, but that does not mean it's good programming style".


Please do not use this topic as a general puzzlers topic. Keep topics specific to one issue. Also consider whether the issue is SCJP specific or whether it is a puzzling aspect of Java which could be equally well discussed in one of our Java In General forums (Beginner, Intermediate, Advanced).

If you are really interested in such puzzlers, treat yourself to a copy of "Java Puzzlers" coauthored by Joshua Bloch.
[ September 02, 2006: Message edited by: Barry Gaunt ]
 
Praveen Babu
Ranch Hand
Posts: 138
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I never wanted it to be a puzzler post but only thought of helping others from the traps that we usually see on the exam. Any way, we will stick to the rules.

Thanks & Regards,
Praveen
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic