This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can we assign a primitive array into Object reference

 
Manju Rao
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

Below code is from K & B Chapter 3's exercise
class Dims {
public static void main(String[] args)
{
int[][] a = {{1,2,}, {3,4}};
int[] b = (int[]) a[1];
Object o1 = a;
int[][] a2 = (int[][]) o1;
int[] b2 = (int[]) o1;
System.out.println(b[1]);
}
}

In this I'm not able to understand that how can we assign an array into an Object reference which is not an array??
Please explain the above code.

Regards
Manju
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because any type of array is considered as an object, so you can assigned any dimensional array to an Object reference.
Try to make some practicals.
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's polymorphism.
Anything in java is an object, extends class Object.
So anything that is not primitive can be assigned to Object.
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let me give you a sample hierarchy to make it simple

 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's see one more sample:


 
kshitij dogra
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit clarifies everything.

nice one
 
Ronald Schild
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:let me give you a sample hierarchy to make it simple



Wouldn't you say that all arrays objects inherit from Object?

We can upcast String[] to Object[] due to casting rules, which has more effect on how we see the type of elements rather than how we
would write out a hierarchy of array classes.





 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ronald you are correct. But I was trying to show that only primitive 1D arrays nherit from Object class. Rest arrays are sub classes of Object[]. So you cannot write this

Object[] arr = new int[5]; //error

This also means that all the multi dimensional arrays are descendants of Object[]. Let me draw the diagram again for completeness

 
Ronald Schild
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:Ronald you are correct. But I was trying to show that only primitive 1D arrays nherit from Object class. Rest arrays are sub classes of Object[]. So you cannot write this

Object[] arr = new int[5]; //error

This also means that all the multi dimensional arrays are descendants of Object[]. Let me draw the diagram again for completeness



I understand what you are saying. My point is that casting an array of object references follows rules that use the class hierarchy
of the element types rather than the array object type.

For a variable SomeObject [][] x = new SomeObject[1][2], if SomeObject inherits from AnotherObject, being able to cast
x to AnotherObject[][] does not mean AnotherObject[][] is part of the hierarchy. There's only SomeObject <- AnotherObject.

The only conversion possible is array -> object. In the above case, it's the object referenced by x that I'm after, which is
an array object. Casting x to AnotherObject[][] makes x still point to the array object, which is treated as such. Reducing
dimensions uses the array -> object conversion. If x would be cast to Object[], the second dimension: SomeObject[]
arrays are cast to class Object. The first dimension remains intact, and x would still be referencing an array object.

Casting second (and up) dimensions into object causes the array to contain elements of type object, and makes the
array reference of Object[] (or with more dimensions).

So, casting int[][] to Object[] uses the any array -> Object conversion plus a type conversion that tells that the
array elements of array are now objects instead of int[] array references. And casting int[][] to Object uses the
same cast that was used for the array elements, namely array -> object.

If multidimensional arrays really had to inherit from Object[] it would mean that there is a conversion from an
array with >2 dimensions to Object[] before there could be a cast to Object. That would give the following issue:

int [][][][] x = new int[1][2][3][4] ;
Object[][] a = x ;

The last 3 dimensions are cast to Object[]. Object[] is placed in Object[], and Object[][] exists.

This, I think, does not happen. There is only the second dimension array reference elements that are
cast to Object references int[][] -> Object.

So there's a cast that has a focus on the type of element, and there's a cast that actually upcasts the
array object to an object. I think only the last one really describes array hierarchy, the rest is element
hierarchy.

/discuss
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic