This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of DevSecOps Adventures: A Game-Changing Approach with Chocolate, LEGO, and Coaching Games and have Dana Pylayeva on-line!
See this thread for details.
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

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: 23956
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.
 
Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic