aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem understanding autoboxing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem understanding autoboxing" Watch "Problem understanding autoboxing" New topic
Author

Problem understanding autoboxing

Jose Monzon
Greenhorn

Joined: Jun 05, 2008
Posts: 8
Hi! I have a mess here with boxing, I don't get it completely and I'm getting some estrange behaviour. Perhaps you guys can throw some light into it.

Please, see the code comments:



I think there's some compile magic that I'm missing.
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

please check the JLS here JLS - Narrowing Primitive Conversion Sec 5.2 It says narrowing int to short, byte and char is allowed if the values are within the legal ranges of them respectively!


In addition, if the expression is a constant expression (ยง15.28) of type byte, short, char or int :

A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable.
A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is :
Byte and the value of the constant expression is representable in the type byte.
Short and the value of the constant expression is representable in the type short.
Character and the value of the constant expression is representable in the type char.
If the type of the expression cannot be converted to the type of the variable by a conversion permitted in an assignment context, then a compile-time error occurs.
If the type of the variable is float or double, then value set conversion is applied to the value v that is the results of the type conversion:

If v is of type float and is an element of the float-extended-exponent value set, then the implementation must map v to the nearest element of the float value set. This conversion may result in overflow or underflow.
If v is of type double and is an element of the double-extended-exponent value set, then the implementation must map v to the nearest element of the double value set. This conversion may result in overflow or underflow.
If the type of an expression can be converted to the type of a variable by assignment conversion, we say the expression (or its value) is assignable to the variable or, equivalently, that the type of the expression is assignment compatible with the type of the variable.

If, after the type conversions above have been applied, the resulting value is an object which is not an instance of a subclass or subinterface of the erasure of the type of the variable, then a ClassCastException is thrown.



also check the following code.


SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem understanding autoboxing