my dog learned polymorphism
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 OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is this conversion vald?" Watch "Is this conversion vald?" New topic

Is this conversion vald?

Rajat Sarkar

Joined: Sep 07, 2008
Posts: 18
public class inttoObject
public static void main(String[] args)
int x=90;
Object o;

How a primitive like 'int' can be assigned to a 'Object' reference?
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11882

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:
subject: Is this conversion vald?
It's not a secret anymore!