basic question

P

Praveen

Hi..All,

This is a question regarding the reference assignment conversion :

Object objRef;
int[] intArray = new int[10];
Object objArray = new Object[3];

Why would this assignment would go through :

objRef = intArray;

and give compile time error on

objArray = intArray;


Regards,
p
 
L

Leif Roar Moldskred

Praveen said:
Hi..All,

This is a question regarding the reference assignment conversion :

Object objRef;
int[] intArray = new int[10];
Object objArray = new Object[3];

Why would this assignment would go through :

objRef = intArray;

and give compile time error on

objArray = intArray;

Basically, every array is a subtype of Object, but "array of int" is
not a subtype of "array of Object."

You can assume that the class hiearchy is effectively as below:

___________
| Object |
-----------
^
|
|
___________
| "Array" |
| (abstract)|
-----------
^ ^
| |
/ \
/ \
___________ ___________
| int[] | | Object[] |
----------- -----------
 
J

Jezuch

U¿ytkownik Praveen napisa³:
Object objRef;
int[] intArray = new int[10];
Object objArray = new Object[3];

Did you mean:
Object[] objArray
?
Otherwise...
Why would this assignment would go through :
objRef = intArray;
and give compile time error on
objArray = intArray;

....this is not what's happening :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top