File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is this conversion vald?

 
Rajat Sarkar
Greenhorn
Posts: 18
  • 0
  • 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
Pie
Posts: 12017
24
Chrome Java Linux
  • 0
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldnt work on Java 1.4
 
Brian Pianczk
Ranch Hand
Posts: 45
  • 0
  • 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.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic