File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How can we assign a primitive array into Object reference Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How can we assign a primitive array into Object reference" Watch "How can we assign a primitive array into Object reference" New topic
Author

How can we assign a primitive array into Object reference

Manju Rao
Greenhorn

Joined: Mar 05, 2008
Posts: 12
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

Joined: Oct 16, 2008
Posts: 952
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.


SCJP 6
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
That's polymorphism.
Anything in java is an object, extends class Object.
So anything that is not primitive can be assigned to Object.


SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

let me give you a sample hierarchy to make it simple



SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Let's see one more sample:


kshitij dogra
Ranch Hand

Joined: Dec 28, 2008
Posts: 39
Ankit clarifies everything.

nice one


SCJP 5.0 - 100%
Ronald Schild
Ranch Hand

Joined: Jun 09, 2008
Posts: 117
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.






Java hobbyist.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

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

Joined: Jun 09, 2008
Posts: 117
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can we assign a primitive array into Object reference