Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

question about number literals  RSS feed

 
Stu Thompson
Hooplehead
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I am getting to grips with literals and am mildly perplexed by something�or have got something wrong. (I am ignoring casting with (type) prefixes here.)

Floating point literals are implicitly of type double.
Integer literals are implicitly of type int.

Floating point numbers can be explicitly typed as double or float with the appropriate literal suffix F, f, D, or d.
BUT, integer literals can only be expressed explicitly as long (L, l) o and cannot be explicitly of typed as int or byte�there is no B, B, I or i literal suffix.

It seems inconsistent to me�maybe I�ve got something wrong?

Thanks for any input.

Stu
[ July 27, 2006: Message edited by: Stu Thompson ]
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Stu,

No, you've got it right. Java basically inherits this behavior from the C language, along with all sorts of other little "why'd they do that?" things. It is indeed inconsistent.

One thing to note, though, is that byte and short literals can always be expressed as an int and then cast to the right type, but longs can be larger than an int, so that wouldn't always work for them; thus long needs a prefix, while byte and short don't.
 
Stu Thompson
Hooplehead
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, thanks. This leads to my next question...

Why is...

byte b = 1;
b = b + 1;

...(understandabley) illegal, but...

byte b = 1;
b += 1;

...IS legal? It's like for compound operators the literal is automatically typed.

Thanks again.

Stu
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In b = b + 1, the RHS is evaluated by converting b to an int, then adding the int 1 to it. This gives an int, which won't fit back into b without a cast.

In b += 1, I guess the compiler can see that the literal 1 fits in a byte.

Something like that anyway. Rubbish, isn't it?
 
Stu Thompson
Hooplehead
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After reading and playing around, I am now well upset about this. Might go cry in a corner for a few hours now.

LEGAL (compile and runtime):
byte b = 4;
b += 222;

ILLEGAL:
byte b = 4;
b = 222;
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't cry -- it makes the moose nervous, and then he drinks too much.

The compound assignment operators, by definition, include a cast to the target type, so that "b += 222" is actually equivalent to "b = (byte) (b + 222)".

Now does it make sense?
 
Stu Thompson
Hooplehead
Ranch Hand
Posts: 136
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK...I don't like it but I'll get over it.

(sniff, sniff)

Thanks.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!