aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About cast of int Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "About cast of int" Watch "About cast of int" New topic
Author

About cast of int

Jack Crifer
Greenhorn

Joined: May 18, 2008
Posts: 29
As we all know , a literal integer (such as 27) is always implicitly an int. add two bytes together and you'll get an int

for example:

⑴ byte b = 27; // complie ok!

⑵ byte b = 3;
byte c = 8;
byte d = b + c; //compile error !

why in ⑴ the compiler automatically narrows the literal value to a byte
but in ⑵ the complier won't?
Paul Prusko
Greenhorn

Joined: Feb 16, 2008
Posts: 25
Hi,

The compiler does narrow the byte b = 3; as it is doing in line 1. However the last line of your code appears to be the real problem here because you are trying to add to bytes which will result in an int so you cannot assign it to byte type variable. Looking at your code, it seems you might get a compile error in line 2 because you define variable b which has already been defined in line 1.

Regards
Paul.
Jack Crifer
Greenhorn

Joined: May 18, 2008
Posts: 29
thank you ,Paul.

what I don't understand it is (byte b = 3) 3 is an int , but we can assign it to byte type variable .

and byte + byte is an int too,why we cannot assign it to byte type variable?
pawni jain
Greenhorn

Joined: Apr 10, 2008
Posts: 22
@jack,


byte b = 3;
byte c = 8;
byte d = b + c; //compile error !


because result of an expression involving int sized or smaller is always int.
So if you add two byte result is int.
if you divide short by int result is int and so on.

you can correct this code as
byte d=(byte)(b+c);
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1282

Hi Jack,

the problem here is that for the compiler it makes a difference if you assign an integer literal like "3" or if you assign an expression like "b + c". For you it may be obvious that "b + c" in this could be stored into a byte variable without loss of information but for the compiler the value of such an expression isn't known at compile time so it has to assume that the expression "b + c" could lead to an overflow if you try to put it in a byte variable. Therefore you have to explicitly cast it to a byte telling the compiler that you know there could be loss of precision and that's OK for you


Marco
Paul Prusko
Greenhorn

Joined: Feb 16, 2008
Posts: 25
Maybe I didn't get the point, Jack.
Anyway, Pawni Jain is right. In addition I can add that when declaring a variable like byte b = 3 (or - in case of byte type - with any other value <= 127) the compiler is smart enough to notice that this number will fit into 8-bit byte so it narrows this - otherwise int literal - implicitely to byte.

Regards.
Paul.
Jack Crifer
Greenhorn

Joined: May 18, 2008
Posts: 29
Thank you all, and thank Marco

For you it may be obvious that "b + c" in this could be stored into a byte variable without loss of information but for the compiler the value of such an expression isn't known at compile time .

and i think this is the point.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About cast of int