Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please explain this array question to me Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please explain this array question to me" Watch "Please explain this array question to me" New topic
Author

Please explain this array question to me

Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Source cathy and sierra
chapter 3


Q7. Given:
1. class Zippy {
2. String[] x;
3. int[] a [] = {{1,2}, {1}};
4. Object c = new long[4];
5. Object[] d = x;
6. }

What is the result?
A. Compilation succeeds.
B. Compilation fails due only to an error on line 3.
C. Compilation fails due only to an error on line 4.
D. Compilation fails due only to an error on line 5.
E. Compilation fails due to errors on lines 3 and 5.
F. Compilation fails due to errors on lines 3, 4, and 5.

Answer:
A is correct, all of these array declarations are legal. Lines 4 and 5 demonstrate that arrays
can be cast.



Can someone explain me why Object c = new long[4]; is getting compiled? It should be a compile time error?
[ November 19, 2008: Message edited by: Himanshu Gupta ]

My Blog SCJP 5 SCWCD 5
Graeme Jenkinson
Greenhorn

Joined: Sep 08, 2008
Posts: 27
Although the statment new long[4] creates an array of primitives of type long, the array itself is an object. This array object is being upcast to the type Object.

Hope that makes sense to you.

thanks,

Graeme
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Thanks. After all array is also an object so it can be assigned to Object reference. Thanks for your explanation.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please explain this array question to me