| 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.
|
 |
 |
|
|
subject: Please explain this array question to me
|
|
|