File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes cast of numeric literal int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "cast of numeric literal int" Watch "cast of numeric literal int" New topic
Author

cast of numeric literal int

Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
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

Joined: Jan 07, 2002
Posts: 2205
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.


Rob
SCJP 1.4
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
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


SCJP Tipline, etc.
Paul Salerno
Ranch Hand

Joined: Jan 17, 2002
Posts: 172
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

Joined: Dec 20, 2001
Posts: 3271
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

Joined: Jan 17, 2002
Posts: 172
Corey this makes total sense. Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: cast of numeric literal int
 
Similar Threads
conversion & casting
Java Question
doubt in primitive assignment...
under what conditions can an int be assigned to a byte?
byte b = 'a'; ???