• 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

Sierra Bates SCJP 6 Ch. 3 No. 3

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sierra Bates SCJP 6 Ch. 3 No. 3

Help me understand this question.



Obviously there is an error in the code at line 4

int[] b = (int[]) a[1];

int[] a is a 2 dimensional array and cannot be cast to the size of a one dimensional array.

But the choices given are

A. 2
B. 4
C. An exception is thrown at runtime
D. Compilation fails due to an error on line 4
E. Compilation fails due to an error on line 5
F. Compilation fails due to an error on line 6
G. Compilation fails due to an error on line 7


If you choose D you are wrong - the code compiles, but an exception is thrown at runtime - so choice C is correct.

How can we tell that the error at line 4 will result in a runtime error vs. a compilation error? On the test we will not have the option of being able to compile this code ourselves, we just have to determine the result from the code.
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

it is ok that this is two dimensional array
consider it array of arrays
so every elements in this array is itself a single dimensional array right?
now
we can assign a single dimensional array to another single dimensional array
so

here we are creating a single dimensional array and assigning a[1](which is single dimensional array to in[] b)
so this does not give compiler error
can you find the cause for the runtime Exception?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


There is no problem with this line. There will be a ClassCastException in some where else in your code. Could you spot it? You can't cast an array of one dimension to other dimension.
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a[1] is an element of a two-dimensional array. Elements of a two-dimensional array are one-dimensional arrays.

so if you have



you can go:


all the elements of a, namely a[0], a[1] and a[2], will be int arrays, NOT ints.

the elements of the elements of a will be ints.

meaning a[1][0] will be 0.
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,



Look at these two lines, line 3 is assigning a single dimensional array to object which is legal as array is an object.
Now when you see at line 4, you are basically trying to convert a single dimensional array to double dimensional
array which is not possible, so the compilation with fail because of the error at line 4.

Hope this helps,


 
Tod Stroszer
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prithvi Sehgal wrote:Hello,



Look at these two lines, line 3 is assigning a single dimensional array to object which is legal as array is an object.
Now when you see at line 4, you are basically trying to convert a single dimensional array to double dimensional
array which is not possible, so the compilation with fail because of the error at line 4.

Hope this helps,




I interpreted the above code like this:

a is a 2 dimensional array

int[][] a = {{1,2,}, {3,4}};

so here you are changing its type to Object


Object o1 = a;

and here you are trying to cast an Object to a 2 dimensional array

int[][] a2 = (int[][]) o1;

-so trying to cast an Object to a 2 dimensional array will give you a ClassCast exception and runtime error?

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As I told you, the problem is with the line below. Check the error message, when you compile.



You are assigning a two dimensional array into one dimensional array. Here is the problem. Check it with your compiler.
 
Tod Stroszer
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see it now, thank you!
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tod Stroszer wrote:I see it now, thank you!



So, did you solved it?
 
Tod Stroszer
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abimaran Kugathasan wrote:

Tod Stroszer wrote:I see it now, thank you!



So, did you solved it?






First o1 is an Object, then it is cast to a 2 dimensional array, then the code attempts to cast a 2 dimensional array to a 1 dimensional array - this causes the runtime exception.


Yes, if you assign an array to a previously declared array reference the array must be of the same dimensions as the array reference you're assigning it to.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And also, have look on your original code, line number 8. Got the difference!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya that is correct ...
int[][]b2 =(int [][])01 ..then it will not show any exception.
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,
I guess i was too busy looking at the correct source code and didn't answer properly.

Best Regards,
 
Greenhorn
Posts: 19
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am unable to understand how the output for b[1] would be 4 if there is no error at line 7?
 
reply
    Bookmark Topic Watch Topic
  • New Topic