• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Doubt in K & B self test

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This from self test given in K & B book chapter 3.

code
-------------------------------------------------------
class Dims{
public static void main(String[] args){
int [] [] a = {{1,2, }, {3,4}};
int [] b = (int [])a[1];
Object o1 = a;
int [] [] a2 = (int [] [])o1;
int [] b2 = (int []) o1; // Line 1
System.out.println(b[1]);
}}
-------------------------------------------------------

The answer is a class cast exception thrown at line 1. If line 1 is removed the output is 4. Can somebody explain how?
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here in the code
int [] b2 = (int []) o1; // Line 1
where o1 is an 2-d array

a single Dimension array cannot accept a 2-d array

Its is given in Chap 3 with examples.

int [] [] // 2-d can accept a 2-d or a 1-d array
int [] // can accept a primitive or 1-d array
 
ram shah
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply...

But, could you pls explain how the output is 4 when line 1 is removed?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by ram shah:
...The answer is a class cast exception thrown at line 1. If line 1 is removed the output is 4. Can somebody explain how?


First, consider what happens if that line is removed. The variable 'a' references a "two-dimensional" array. In Java, a multi-dimensional array is really just an array of arrays. So 'a' references an array where the first element a[0] references an array containing {1, 2}, and the second element a[1] references an array containing {3, 4}. In the above code, a[1] is assigned to 'b' (and note the explicit cast in that line is not necessary). Now do you see why the output is 4?

Now consider what happens when line 1 is present. The variable 'o1' is assigned a reference to 'a', which -- as we saw above -- actually references an array of arrays. But in line 1, we try to cast that reference to an array of ints. Do you see what the ClassCastException occurrs?

PS: Please use Code Tags when posting code.
 
To do a great right, do a little wrong - shakepeare. twisted little ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic