• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multi dimension array confusion

 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Zeeshan Sheikh
Ranch Hand
Posts: 144
Java MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


o1 is of type Object and int is primitive.
 
Vishal Hegde
Ranch Hand
Posts: 1077
Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zeeshan Sheikh wrote:

o1 is of type Object and int is primitive.


But casting is done right???
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Manoj Kumar Jain
Ranch Hand
Posts: 195
Java Linux Oracle
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic