Casting a primitive int to a Number

Posted by Tamer on Stack Overflow See other posts from Stack Overflow or by Tamer
Published on 2010-03-17T19:04:49Z Indexed on 2010/03/17 19:11 UTC
Read the original article Hit count: 315

Filed under:
|
|

Let's say that I have the following:

int a = 2;
Number b = (Number) a;

System.out.println(b); // Prints 2

http://java.sun.com/docs/books/jls/first_edition/html/15.doc.html#238146 says that a primitive value may not be cast to a reference type. Does Java know to create an Integer from the primitive int and then cast to the superclass? How exactly does Java handle this behind the scenes? Thanks!

© Stack Overflow or respective owner

Related posts about java

Related posts about casting