| Author |
Is this conversion vald?
|
Rajat Sarkar
Greenhorn
Joined: Sep 07, 2008
Posts: 18
|
|
public class inttoObject
{
public static void main(String[] args)
{
int x=90;
Object o;
o=90;
System.out.println(o);
}
}
How a primitive like 'int' can be assigned to a 'Object' reference?
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9948
|
|
|
I would guess that '90' is being autoboxed into an Integer, and that is getting assigned to the Object reference.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Sridhar Santhanakrishnan
Ranch Hand
Joined: Mar 20, 2007
Posts: 317
|
|
|
Wouldnt work on Java 1.4
|
 |
Brian Pianczk
Ranch Hand
Joined: Jan 26, 2009
Posts: 45
|
|
I think what is happening here is; since everything is a subclass of Object the compiler is seeing Object 0 as 90. the int x = 90; is not relevant?
I could be wrong, just my take.
This compiles and outputs 90.
|
 |
 |
|
|
subject: Is this conversion vald?
|
|
|