Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Array- can't understand the code.

 
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Can some one please explain what's going on this code.
What does line 4 mean.
In line 5 why is a 2-dimensional array being assigned to a variable of type Object.
What is line 6&7 . Some kind of casting?

I got a runtime error(Class Cast exception) because of line 7 which if removed will give the output 4.

[Question from K&B]
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Code Explained with comment

1 class Dims {
2public static void main(String[] args) {

3 int[][] a={ {1,2}, {3,4} }; // Creating two dimensional Array with
//a[0] = {1,2} and a[1] = {3,4}
4int[] b= (int[]) a[1]; // assigning a[1] to b
5Object o1=a; // Remember a is also an object so
// upcasting to Object
6int[][] a2=(int[][])o1; // assigning a to a2 with downcasting
7int[] b2=(int[]) o1; // Run Time exception as ol can only be
//type casted to 2- d array
8System.out.println(b[1]);
}

}

Thanks and Regards,
Gaurav Joshi
 
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int[][] a={ {1,2}, {3,4} };
//Declares an array object referred by a. a's array object contains references to two array objects:
a[0] --refers to--> {1,2}
a[1] --refers to--> {3,4}
Therefore we actually are creating 3 array objects on the heap here. One referred by a, second by a[0] and third by a[1].



4int[] b= (int[]) a[1];
// b refers to the 1D array object referred by a[1], so casting allowed & not necessary here.

5Object o1=a;
//Since any array object is also an Object, we can refer to a by an Object type variable o1.

6int[][] a2=(int[][])o1;
//Since o1 actually refers a 2D array type, we CAN cast it back to a 2D array type.

7int[] b2=(int[]) o1;
//Since o1 actually refers a 2D array type, we CANT cast it back to a 1D array type.

8System.out.println(b[1]);
//b --> a[1]. Hence b[1] = a[1][1] = 4!




Can some one please explain what's going on this code.
What does line 4 mean.
In line 5 why is a 2-dimensional array being assigned to a variable of type Object.
What is line 6&7 . Some kind of casting?

I got a runtime error(Class Cast exception) because of line 7 which if removed will give the output 4.

--#@ I hope that cleared a bit @#--
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Gaurav Joshi And Nadeem Khan`s reply are all right.
I think the problem is clear.
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks alot guys..That cleared alot!
Just one question..

Object o1=a;

Can a two dimensional array be assigned to o1 of type Object (which is not 2-dimensional) and simply an object.

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

Why do we need to cast o1(ie. a as Object o1=a) when it is already 2-dimensional.
[I am getting an error of incompatible type when I am trying to compile it with out casting.]


Thanks.
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Can a two dimensional array be assigned to o1 of type Object (which is not 2-dimensional) and simply an object.



All arrays are objects. So, a two dimensional array, which is an array of array, is an object.


Why do we need to cast o1(ie. a as Object o1=a) when it is already 2-dimensional.
[I am getting an error of incompatible type when I am trying to compile it with out casting.]



All arrays are objects, but not all objects are arrays. So the Java compiler doesn't know if an object can be casted to an array, implicitedly.

Henry
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.
That made it clear.
 
If tomatoes are a fruit, then ketchup must be a jam. Taste this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic