aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Confusing array declarations (was:A Good One) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Confusing array declarations (was:A Good One)" Watch "Confusing array declarations (was:A Good One)" New topic
Author

Confusing array declarations (was:A Good One)

Praveen Babu
Ranch Hand

Joined: Jul 30, 2006
Posts: 138
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

Joined: Aug 25, 2006
Posts: 1274
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


all events occur in real time
Praveen Babu
Ranch Hand

Joined: Jul 30, 2006
Posts: 138
Its better than mine.
bing marquez
Greenhorn

Joined: Aug 06, 2006
Posts: 11
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

Joined: Aug 25, 2006
Posts: 1274
praveen babu recently wrote
Its better than mine.

Different, but not better.
Bu.
Praveen Babu
Ranch Hand

Joined: Jul 30, 2006
Posts: 138
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

Joined: Aug 03, 2002
Posts: 7729
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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Praveen Babu
Ranch Hand

Joined: Jul 30, 2006
Posts: 138
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confusing array declarations (was:A Good One)