aspose file tools*
The moose likes Java in General and the fly likes Multi dimension array confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Multi dimension array confusion" Watch "Multi dimension array confusion" New topic
Author

Multi dimension array confusion

Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055


Dear Ranchers,

I have confusion with the above code as per my understanding.


The very first line in Main method

-An Array is created in which the 0 index is occupying the ints 1, 2 and the 1st Index is occupying 3 and 4
- an int array Object b is assigned values of {3,4} from a object array variable(using typecasting as b is a single dimension arry and a is a multiple dimensional array)
- A Generic Object 01 refernce variable is created and is assigned with a's reference variable
-a2 Multi dimensional array object is created and a refernce variable of 01 is assigned to a2
-b2 int array refernce object is created and o1 is assigned to b2(using casting) // but why this line gives class Casting exception we are typecasting it right?.. the same thing we did in line 2 too and there was no exception in that case?


http://www.lifesbizzare.blogspot.com || OCJP:81%
Zeeshan Sheikh
Ranch Hand

Joined: Nov 20, 2011
Posts: 144



o1 is of type Object and int is primitive.


MySQL Blog
http://mysqlearner.blogspot.com/
Vishal Hegde
Ranch Hand

Joined: Aug 01, 2009
Posts: 1055

Zeeshan Sheikh wrote:

o1 is of type Object and int is primitive.


But casting is done right???
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3682
    
  16
Vishal Hegde wrote:- an int array Object b is assigned values of {3,4} from a object array variable(using typecasting as b is a single dimension arry and a is a multiple dimensional array)

a is a multiple dimension array, but a[1] is a single dimension array so no cast is needed

Vishal Hegde wrote:-b2 int array refernce object is created and o1 is assigned to b2(using casting) // but why this line gives class Casting exception we are typecasting it right?.. the same thing we did in line 2 too and there was no exception in that case?

o1 is referring to the same object as a which is a multiple dimension array and you are trying to cast it to a single dimension array so you get a ClassCastException.


Joanne
Manoj Kumar Jain
Ranch Hand

Joined: Aug 22, 2008
Posts: 191

First you need not to cast the a[1] to assign it to b, because its the single dimension array type.
when you assigned a to Object o1 you are assigning the subtype of reference to its super class, however o1 is the Object type but actually the o1 is int[][] type so it can perform/operate like int[][].

when you are assigning o1 to a2 with explicit type casting you are asking compiler that you knows that the object is of int[] [] type and will work fine if treated like int [] []. same thing happen when you assign o1 to b2 (You are saying to compiler that I know it will work fine this is of type int []).

But at run time when compiler checks for type it finds that the told lie to it. and o1 is not int[] type and it annoyed.

Do not wait to strike till the iron is hot; but make it hot by striking....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multi dimension array confusion