• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

About cast of int

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@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);
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic