This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is this conversion vald?

 
Rajat Sarkar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12145
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would guess that '90' is being autoboxed into an Integer, and that is getting assigned to the Object reference.
 
Sridhar Santhanakrishnan
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldnt work on Java 1.4
 
Brian Pianczk
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic