aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes AutoBoxing Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "AutoBoxing Doubt" Watch "AutoBoxing Doubt" New topic
Author

AutoBoxing Doubt

harsh sahay
Greenhorn

Joined: May 13, 2011
Posts: 13


Hi all,
My doubt with this code is why auto-boxing with Long d=4 is failing as compared to Byte,Short,and Integer where it is a success? I don't understand why we need to explicitly specify the value as 4L for it to work. Since 4 is an int type shouldn't it be boxed automatically to Long? Please clarify.


OCPJP6
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18103
    
  39

harsh sahay wrote:
Hi all,
My doubt with this code is why auto-boxing with Long d=4 is failing as compared to Byte,Short,and Integer where it is a success? I don't understand why we need to explicitly specify the value as 4L for it to work. Since 4 is an int type shouldn't it be boxed automatically to Long? Please clarify.



Weirdly, I think that the easiest way to understand it, is to look at the specification (section 5.2 to be exact) ...

Java Language Specification wrote:5.2 Assignment Conversion
Assignment conversion occurs when the value of an expression is assigned (§15.26) to a variable: the type of the expression must be converted to the type of the variable. Assignment contexts allow the use of one of the following:

an identity conversion (§5.1.1)
a widening primitive conversion (§5.1.2)
a widening reference conversion (§5.1.5)
a boxing conversion (§5.1.7) optionally followed by a widening reference conversion
an unboxing conversion (§5.1.8) optionally followed by a widening primitive conversion.

If, after the conversions listed above have been applied, the resulting type is a raw type (§4.8), unchecked conversion (§5.1.9) may then be applied. It is a compile time error if the chain of conversions contains two parameterized types that are not not in the subtype relation.

Discussion
An example of such an illegal chain would be:

Integer, Comparable<Integer>, Comparable, Comparable<String>

The first three elements of the chain are related by widening reference conversion, while the last entry is derived from its predecessor by unchecked conversion. However, this dis not a valid assignment conversion, because the chain contains two parameterized types, Comparable<Integer> and Comparable<String>, that are not subtypes.

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.


First, notice that the only two combinations of implicit casts and autoboxing are...

a boxing conversion (§5.1.7) optionally followed by a widening reference conversion
an unboxing conversion (§5.1.8) optionally followed by a widening primitive conversion.


So, technically, only the Integer assignment (line 10 in the example), the Long assignment (line 12), and Float assignment (line 14) are allowed, as those assignments do not have any implicit casts -- all the other assignments should fail as it is not valid to cast a primative to be follow by a boxing operation.


However, you are also using literals in your example; int, long, float, and double literals are compile time constants. And there are special rules regarding compile time constant casting and boxing. Specifically....

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.


And with these special rules, line 8 and line 9 of your example is also allowed.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
fadi aboona
Ranch Hand

Joined: Apr 25, 2010
Posts: 71
harsh sahay wrote:
Hi all,
My doubt with this code is why auto-boxing with Long d=4 is failing as compared to Byte,Short,and Integer where it is a success? I don't understand why we need to explicitly specify the value as 4L for it to work. Since 4 is an int type shouldn't it be boxed automatically to Long? Please clarify.


Because the compiler would first box the 4 to Integer then it would be impossible to assign Integer into Long the IS-A test fails, Integer IS-A Long? No
harsh sahay
Greenhorn

Joined: May 13, 2011
Posts: 13

Java Language Specification wrote:5.2 Assignment Conversion
Assignment conversion occurs when the value of an expression is assigned (§15.26) to a variable: the type of the expression must be converted to the type of the variable. Assignment contexts allow the use of one of the following:

an identity conversion (§5.1.1)
a widening primitive conversion (§5.1.2)
a widening reference conversion (§5.1.5)
a boxing conversion (§5.1.7) optionally followed by a widening reference conversion
an unboxing conversion (§5.1.8) optionally followed by a widening primitive conversion.

If, after the conversions listed above have been applied, the resulting type is a raw type (§4.8), unchecked conversion (§5.1.9) may then be applied. It is a compile time error if the chain of conversions contains two parameterized types that are not not in the subtype relation.

Discussion
An example of such an illegal chain would be:

Integer, Comparable<Integer>, Comparable, Comparable<String>

The first three elements of the chain are related by widening reference conversion, while the last entry is derived from its predecessor by unchecked conversion. However, this dis not a valid assignment conversion, because the chain contains two parameterized types, Comparable<Integer> and Comparable<String>, that are not subtypes.

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.

Thanks Henry, this was really informative.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: AutoBoxing Doubt
 
Similar Threads
Overriding Doubt
doubt in primitive assignment...
Overloading doubt
Conversion doubt
primitive widening doubt