• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array Question

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Just got across this question in one of the mock exams.. and not surprisingly i gave the answer wrong...

What is the result of attempting to compile and run the above program?
a. Compiler error at line 3.
b. Compiler error at line 4.
c. Compiler error at line 5.
d. Compiler error at line 6.
e. Compiler error at line 7.
f. Run time error.
g. None of the above.
I gave the answer as B, but the correct answer is G. theres absolutely no problems with the code.. According to me, u can assign a Two dimensional array to one D array.. so i thought line 4 will give a compilation error.. So where am i wrong in this ?
Regards,
Harry
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that accordirng to JLS 5.5 (Object[]) a.clone(); should produce a ClassCastException.
Let's see how the 1.4.2_01 compiler is fooling us:

I write (int[]) new int[] {} and the compiler cast the object to int[]
However I write (Integer[]) new Integer[] {}; and the compiler cast the object to Integer
 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure can't state this with authority, my sense of what's happening is that an int[][] is an array of int arrays, i.e., it's an array of Objects, so everything in the example works.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It sure won't cause any compile-time error in line 4. You know, a.clone() returns an object, while an object can be cast to anything at least during compile time. No exception will occur at run time, too . As Steve said, a.clone is actually an array of int[], it can be assigned to an array of object. In fact, you can even write line 4 like this:
Object[] obj = (int[][]) a.clone();
It still works correctly.
Hope this help.
 
Harry Singh
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so what should be the output of this array and why.. which all int values will be there in a single D array. Will al the int values be there?
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Harjindar
So, if I try to explain this output,
lets consider int[][]a = new int[][]{{1,2},{0,1,2},{-1,0,2}};
as
int[] a1 = new int[]{1,2};
int[] a2 = new int[]{0,1,2};
int[] a3 = new int[]{-1,0,2};
int[][] a = new int[][]{ a1, a2, a3};
Now, when we casted a.clone() to Object[] it would become array of 3 objects which are a1,a2 a3 which are again arrays.
So in the for loop we have, obj.length would be = 3. It means it will go from 0..2. Now, here is what we have,
obj[0] = a1;
obj[1] = a2;
obj[2] = a3;
So far so good?
Now, in the loop we are casting each obj[i] in int[] and then printing i th element (the loop index) so first time it would print,
obj[0][0],
second time,
obj[1][1]
third time,
obj[2][2]
right?
you get my point? So the output would be 1,1,2.
Regards
Maulin
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Lei.
__________________________________________________________________________
I understand that a multimensional array can be thought as an array of objects. However the runtime type of the object returned by clone is still int[][].
The cast "(Object[]) new int[][] {{}};" conflicts --to my knowledge-- with JLS 5.5
Isn't suspicious that the compiler checks this cast (int[][]) new int[][] {{}} against the type int[][], but in this one (Object[]) new int[][] {{}} it cheks against Object ?
 
Lei Wu
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To Jose Botella:

The cast "(Object[]) new int[][] {{}};" conflicts --to my knowledge-- with JLS 5.5


No, it doesn't conflicts with JLS 5.5.
Now, let's first look at what JLS 5.5 says:
<1> If S is an array type SC[], that is, an array of components of type SC :
<1.1> If T is an array type TC[], that is, an array of components of type TC, then a compile-time error occurs unless one of the following is true:
<1.1.1> TC and SC are reference types and type SC can be cast to TC by a recursive application of these compile-time rules for casting.
So, here SC[] is int[][], which is an array of int[], SC is hence int[]. TC[] is Object[], and TC is of type Object. Since int[] can be cast to Object at compile-time, it is correct to cast int[][] to Object[], according to the above rule. Actually, it's a narrowing reference conversion from int[][] to Object[].
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are right Lei.
I just though SC was int[][] but it is int[]
In fact no checking is necessary at runtime according to


The cast can be determined to be correct at compile time. A cast from the compile-time type S to compile-time type T is correct at compile time if and only if S can be converted to T by assignment conversion (�5.2)


Thank you for adjusting my knowledge!
___________________________________________________________________________
Object[] o = new int[][] {{}};
it is a widdening reference conversion, you do not need a cast for the assignment.
[ September 24, 2003: Message edited by: Jose Botella ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic