This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan 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 | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
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: 10909
    
  12

I would guess that '90' is being autoboxed into an Integer, and that is getting assigned to the Object reference.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
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
R & H Bonus1 question#36
Array of Objects
cast
Which overloaded method will be called if i am passing null as argument?
Kathy's Book- Chapter7 Q5- hashCode method