Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Java 6.0 widening conversion bug?

807588Jul 17 2009 — edited Jul 17 2009
a)
Byte test = (byte)10; // works
Short test = (byte)10; // works
Integer test = (byte)10; // works
Long test = (byte)10; // doesn't work
Float test = (byte)10; // doesn't work
Double test = (byte)10; // doesn't work

Summary: seems like it does a promotion to a max of Integer

b)
final byte x = 10; Byte y = x; // works
final byte x = 10; Short y = x; // works
final byte x = 10; Integer y = x; // works
final byte x = 10; Long y = x; // doesn't work
final byte x = 10; Float y = x; // doesn't work
final byte x = 10; Double y = x; // doesn't work

Summary : seems like it does a promotion to a max of Integer and only if the variable is final.
Question: Why does it need a variable to be final to be promoted? Final is used for constant expression to be used for implicit narrowing conversion.

c)
byte x = 10; Byte y = x; // works
byte x = 10; Short y = x; // doesn't work
byte x = 10; Integer y = x; // doesn't work
byte x = 10; Float y = x; // doesn't work
byte x = 10; Double y = x; // doesn't work

Without finals .. it fails at short.
Strange ..
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 14 2009
Added on Jul 17 2009
3 comments
55 views