• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About Array reference assignments for One-D arrays

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And ...
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anybody here???
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because an array is a Object.
 
saima kanwal
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 :



 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks saima kanwal & Wouter Oet
 
Honey Rathore
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Kamil Wojcik
Ranch Hand
Posts: 78
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anu,
it's because int[] is not a subtype of Object[]
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 78
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 )
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic