aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About Array reference assignments for One-D arrays 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 "About Array reference assignments for One-D arrays" Watch "About Array reference assignments for One-D arrays" New topic
Author

About Array reference assignments for One-D arrays

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

I got the example from here.



It compiles & runs fine! And.... It's said that in K&B's book,



When you assign an array to a previously declared array reference, the array you're assigning must be the same dimension as the reference you're assigning it to. ...


What is the differences? Any suggestions?


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

And ...
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Anybody here???
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

That's because an array is a Object.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
Yes Wouter is correct. Array is itself an object , so that is the reason why the following is legal:



or

So it is also legal

Object[] o = new int[] {1,2,3}; //case 1
and
Object[] o = new int[][]{{1,2}.{3.4,5}};//case 2

case 1 : an array is created with 3 Object type reference variables, but the actual instances are int in them.so each Object instance contains one int .

case2: here an array is created with 2 object reference variable s, but the actual instances are int array. So each Object reference of the array contains an int array , but since the reference is single dimensional , the compiler does not permit you to search beyond that.That is why you are getting a compiler error.

The following code may help you :




SCJP 6.0 (86%) SCWCD 5 (94%)
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

saima kanwal wrote :

case 1 : an array is created with 3 Object type reference variables, but the actual instances are int in them.so each Object instance contains one int .


I think, it's a primitive array, of course, the array is a object! But it holds the actual int primitives...??// Correct??/

Since the array is a object (also extends Object), we can upcast it & can be referenced by a object reference variable. For our purposes later, Can we down cast it to behave as array???

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

For other types of array, rather than Object type, we can't do the above operation? Like?


OK? Confirm?? Because every array extends Object, that's why we can able to assign it to a Object reference variable!
saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
Since the array is a object (also extends Object), we can upcast it & can be referenced by a object reference variable. For our purposes later, Can we down cast it to behave as array?


Yes i think we can downcast it , because the following code works fine , where i tried to downcast the object reference to array:

Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Abimaran Kugathasan wrote:For other types of array, rather than Object type, we can't do the above operation? Like?


OK? Confirm?? Because every array extends Object, that's why we can able to assign it to a Object reference variable!


What about this?
saima kanwal
Ranch Hand

Joined: Oct 05, 2009
Posts: 148
Abimaran Kugathasan wrote:
Abimaran Kugathasan wrote:For other types of array, rather than Object type, we can't do the above operation? Like?


OK? Confirm?? Because every array extends Object, that's why we can able to assign it to a Object reference variable!


What about this?


Yes you are right.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Thanks saima kanwal & Wouter Oet
Honey Rathore
Greenhorn

Joined: Nov 11, 2009
Posts: 7
saima kanwal wrote:
Since the array is a object (also extends Object), we can upcast it & can be referenced by a object reference variable. For our purposes later, Can we down cast it to behave as array?


Yes i think we can downcast it , because the following code works fine , where i tried to downcast the object reference to array:



yes you can downcast it
but you have to cast it into a proper type

after int[] i = new int[]{1,2,3};
Object o = i;
if you use o.length; or perform any operation using 'o' instead of 'i'.It won't compile.
that's why you have to cast it to int[]
that's why
int[] a = (int[])o;
for (int in:a)
System.out.println(in);
works fine.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Honey Rathore wrote :

if you use o.length; or perform any operation using 'o' instead of 'i'.It won't compile.


Yaa, Correct! Since the Object don't have the variable length, we can't use it. That's why it gave a Compilation Error!
Anup Om
Ranch Hand

Joined: Dec 30, 2009
Posts: 62
saima kanwal wrote:
So it is also legal

Object[] o = new int[] {1,2,3}; //case 1
case 1 : an array is created with 3 Object type reference variables, but the actual instances are int in them.so each Object instance contains one int .

[/code]



Object[] o = new int[] {1,2,3}; is giving me a compilation error.
incompatible types
found : int[]
required: java.lang.Object[]
Object[] o = new int[] {1,2,3};

Please correct if I have missed something and why is it illegal?. Thank You.


SCJP6
Kamil Wojcik
Ranch Hand

Joined: May 19, 2008
Posts: 78

Anu,
it's because int[] is not a subtype of Object[]


SCJP 6.0 Passed | SCWCD Passed | PSM in progress...
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

There are some problems with these topics....

1)

2)

3)

4)

5)

Please some body explain the stuff regarding to line 3. Thanks in Advanced
Kamil Wojcik
Ranch Hand

Joined: May 19, 2008
Posts: 78

Abimaran Kugathasan wrote:There are some problems with these topics....

3)

Please some body explain the stuff regarding to line 3. Thanks in Advanced


Read my post above.

Maybe this can help: HERE

Abimaran, answer to yourself that is int[] and int[][]
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Abimaran, the array hierarchy looks something like this

There is a reason for this hierarchy being this way. int[] is a reference to an array and Object[] is an array of references. Thus int[] is not assignable to Object[], as int[] is not an array of references. int[][] can be considered a 2-D int array, or better as an array of int arrays. Thus int[][] becomes an array of references (here the references are 1-D int arrays). This is why int[][] is assignable to Object[]. String[] is an array of references, so it is assignable to Object[]. String[] is an array of references of type String, so String[][] is not assignable to it, as String[][] is an array of references but the references are themselves arrays of Strings. Object[][] is also an array of arrays, so it can point to String[][]. The special case with an Object array is that it can store references of any type. So Object[] may have elements which are Strings, Integers, String array, Integer array, array of String arrays etc. This is why String[], String[][], String[][][] are all assignable to Object[]. So the rule in the K&B book which says
When you assign an array to a previously declared array reference, the array you're assigning must be the same dimension as the reference you're assigning it to.

is not applicable to Object[] as it is free to point to an array of any number of dimensions...

(I hope this makes sense, because it didn't make much sense even to me when I wrote it )


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About Array reference assignments for One-D arrays