• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

output not clear

 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please see the following code:Concept of arrays is clear to me but when it comes to these kind of questions i am stuck.I think I am messed up with arrays.Please clear my understanding.

1. class Dims {
2. public static void main(String[] args) {
3. int[][] a = {{1,2,}, {3,4}};
4. int[] b = (int[]) a[1];(---> not clear a[1] which is the 2nd element of 2 dimensional array 'a' should be a ref to an array of int then how this assignment is valid)
5. Object o1 = a;( a is a reference to 2 dim array and its being assigned to an object o1)
6. int[][] a2 = (int[][]) o1;(---> again the same doubt as above)
7. int[] b2 = (int[]) o1;
8. System.out.println(b[1]);
9. } }

The output is:
A ClassCastException is thrown at line 7 because o1 refers to an int[][]
not an int[]. If line 7 was removed, the output would be 4.
 
Ranch Hand
Posts: 342
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi rakhee,

a[0], a[1] are one dimensional int arrays and it holds

a[0] = {1, 2}
a[1] = {3, 4}

so line 4 is no problem, even the cast is unnecessary und we have b[1] = 4 = a[1][1]. This explains the output when line 7 is removed.

Line 6: o1 refers to the same object as a, that is a two dimensional int array. So the cast is legal (and necessary).
Line 7: As already explained, o1 refers a two dimensional int array. So you can't cast it to an one dimensional int array.
[ July 15, 2008: Message edited by: Ralph Jaus ]
 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check this link..
I had the same problem earlier.


https://coderanch.com/t/267921/java-programmer-SCJP/certification/Array-understand-code
 
rakhee gupta
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.Understanding is clear now.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
rakhee, when you copy a question from a book or mock exam, we require that you quote your sources. So, please tell us where you copied it from.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic