Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cast of numeric literal int

 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
a while back we had discussed the fact that this statement does not need casting:
Case 1:
int i = 100
byte b = i // i is a constant and fits into byte
Case 2:
int i = 12
byte b = i // is this legal?
Case 3:
final char c = 10
byte b = c // is this legal?
Case 4:
final short s = 5
byte b = s // is this legal?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Salerno:
a while back we had discussed the fact that this statement does not need casting:
Case 1:
int i = 100
byte b = i // i is a constant and fits into byte

No, this will give you an error! You can do this though:
byte b = 100;

Case 2:
int i = 12
byte b = i // is this legal?

No. I think you're confusing literals and variables. The compiler has no clue what the current value in a variable is when you compile it, so it won't let you make narrowing assignments without an explicit cast. However, if the compiler can tell at compile time what the value is, as with literals and static final variables, then it will allow implicit narrowing assignments provided the value actually fits in the variable.

Case 3:
final char c = 10
byte b = c // is this legal?

Yes. The value in c cannot change, and the compiler can tell what the value is at compile time, so it will allow this.


Case 4:
final short s = 5
byte b = s // is this legal?

Yes, for same reason as above.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A similar topic that Val caught me on recently is that, even though the compiler will check to see if a literal fits into a variable for assignment, like this:

This does not happen when calling methods. Take the following example:

This will cause a compiler error because the method doSomething() is expecting a byte, but it is being called with an int. The compiler, in this case, doesn't check to see if the value is within the bounds of the parameter. That check is only performed on assignment.
Corey
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corey or someone could you elaborate on this?
byte b = 127; // Legal
byte c = 128; // Compiler Error
Whats the rule?
TIA
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Salerno:
Corey or someone could you elaborate on this?
byte b = 127; // Legal
byte c = 128; // Compiler Error
Whats the rule?
TIA

A byte is only 8 bits. That means that it has a range of -128 to 127. So, while the number 127 fits is a byte, 128 does not (it requires 9 bits). The value 128 is 10000000 is binary, which is 8 bits, but we reserve the leftmost bit for the sign bit, so we need 9 bits to store 128.
Make sense?
Corey
 
Paul Salerno
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Corey this makes total sense. Thanks!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic