This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Is this conversion vald? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Is this conversion vald?" Watch "Is this conversion vald?" New topic
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
    
    6

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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is this conversion vald?
 
Similar Threads
cast
R & H Bonus1 question#36
Kathy's Book- Chapter7 Q5- hashCode method
Array of Objects
Which overloaded method will be called if i am passing null as argument?