Skip to Main Content

Java HotSpot Virtual Machine

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!

primitive literals

843829Feb 20 2008 — edited Feb 21 2008
Hello,

I didn't know where to put it, since I couldn't find a JLS sub-forum.

Consider this code:
int i0 = -1; // negation of integer "1", ie. -1
int i1 = 0xFFFFFFFF; // integer "negative 1" in hex
int i2 = 037777777777; // integer "negative 1" in oct
// all variables represent the same binary int value, where all 32-bits are set to '1'

// (note, all of that works for long as well -- with all 64 bits set to '1', respectively)
Now, the following however doesn't work quite as well
// this is OK, though it shouldn't
short s0 = -1; // short "negative 1", ie. -1.

// the following won't work
short s1w = 0xFFFF; // wrong, as it represents an integer, which requires me to explicitely cast
short s2w = 0177777; // wrong, as well

// that's how it has to be done
short s1r = (short) 0xFFFF; // explicitely casting...
short s2r = (short) 0177777; // ...int to short

// if you leave the highest bit (sign), everthing is fine however
short s3 = 0x7FFF; // binary '0111...111'
short s4 = 077777; // binary '0111...111'
JLS �3.10.1 doesn't mention '-' as part of an integer literal, JLS �15.15.4 however qualifies it as the unary minus operator and mentions an unary numeric promotion of the operant prior to the actual operation, which in turn is described in JLS �5.6.1, quote: +"[..] if the operand is of compile-time type *byte*, *short* or *char* unary numeric promotion promotes it to a value of type *int* [..]"+
Now, considering that the literal +1+ certainly doesn't represent a long or floating point value, it must be of a smaller type, such as int, char, short or byte. However, due to the unary numeric promotion it will become an int after all, thus resulting in an int after the unary operation, and thus wouldn't simple fit in a small type. Am I leaving out something essential here?

Well, maybe the reason is in a later paragraph of JLS �15.15.4, quote +"For integer values, negation is the same as subtraction from zero. [..]"+, which doesn't describe a short-cut for the unary operation or how to carry it out, but rather tries to point out, that there is no negation of any maximum negativ integer value due to overflow.

That, in turn, takes me to another question: Why can '-1' be considered a short value (as in the prior reasoning), while '-Short.MIN_VALUE' represents an int value (as supposed to due to unary promotion)?

Furthermore, why is it disallowed to use the literals +0xFFFF+ and +017777+ in a short context?
Strictly speaking: Why do those literals represent int values?
Or from the opposing point of view: Why is it allowed to set the sign-bit via literal representations in hex or oct for int values, such as in +0xFFFFFFFF+ and +037777777777+ ?



(I haven't even considered byte and char so far)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 20 2008
Added on Feb 20 2008
3 comments
299 views