Does the book really say those three boxings work?campbell@queeg:~/java$ javac BoxTest.java
BoxTest.java:6: incompatible types
found : short
required: java.lang.Integer
Integer y = x;
^
BoxTest.java:11: incompatible types
found : short
required: java.lang.Integer
Integer iRef3 = (short)10;
^
BoxTest.java:17: incompatible types
found : short
required: java.lang.Integer
Integer y = x;
^
3 errors
SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
I love this place!
JLS->5.2 wrote: 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.
SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Kim Ming Yap wrote:
Without finals .. it fails at short.
Strange ..
a)
Byte test = (byte)10; // works (just boxing)
Short test = (byte)10; // works (see JLS, but may not work on some JVMs)
Integer test = (byte)10; // works (see JLS, but may not work on some JVMs)
Long test = (byte)10; // doesn't work (see JLS)
Float test = (byte)10; // doesn't work (see JLS)
Double test = (byte)10; // doesn't work (see JLS)
Summary: seems like it does a promotion to a max of Integer
Kim Ming Yap wrote:However the *compile constants* is only meant for implicit narrowing comversion. You dont need a compile time constants for widening conversion .. it doesnt make sense for a widening conversion to have a compile constants. That's what i understand from books by Mughal and Katherine
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.
As you can see .. it widens first and then box!
So does (§15.28) applies for this case?
Kim Ming Yap wrote:BINGO!!! That's what confuses me!! Holy crap! Thanks Henry anyway
Kim Ming Yap wrote:Well mine works ..
I think .. (again like i said .. i think .. )
Integer test = (byte)10; // works *clause A*
The 10 is an int and definitely a constants .. it cast to a byte .. but becos the target variable is not byte, char or short hence it does not meet the JLS5.2 constant expression requirements .. hence it just autobox it.
Kim Ming Yap wrote:
Long x2 = 10l; // this works .. which is fine .. it just autobox
Long x3 = (byte)10l; // this fail .. thought it would be the same as the very above *clause A*
Anyway what's the reason that clause A should fail? is it cos it can only be narrowed to a max of Short? Why cant it be just taken as just plain old autoboxing since it doest meet the JLS5.2 criteria
This is one big mess and damn confusing ..
The link Vijitha Kumara helpfully posted yesterday points out different behaviour on Eclipse.Henry Wong wrote:For me, it works with eclipse, but when I use javac from the command line, I get...
What are you saying? I thought you said that Santa gave you that. And this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|