• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why cast int to byte?

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

the following compiles though the right side is an int:


The following does not compile

But the right side is an int as well.

It does not sound logic to me that the two code snippets behave different even though in both I try to assign an int to a variable of type byte.
Why does the compiler accept the first but refuses the second?
 
Greenhorn
Posts: 16
Eclipse IDE Firefox Browser Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first two assignements work, because the compiler can check, wether those two int literals(!) are in the range of a byte or not. With the third assignment the compiler only sees byte d = an int + an int and not the actual values these two int variables hold and therefore need to be explicitly downcasted to a byte. So it would work like this: byte d = (byte) (b + c);
 
Ranch Hand
Posts: 51
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler can only check literal assignments. For a calculation as in the case for line 3, the compiler will have to perform the calculation (effectively running the code which is not its job) to check if the resultant int can fit in the byte assignment. Therefore in order to be rather safe than sorry, it asks for a cast to byte. Does it make sense?
 
yusuf Kaplan
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thorsten Schneider wrote:With the third assignment the compiler only sees byte d = an int + an int


I think the compiler sees not an int + an int but rather a byte + a byte. But as with Sierra & Bates the result of this expression is an int, right?
Anyway, I think I got the problem. The compiler can not assure that the result of this expression is less then 128. If it could, it would compile. Is that true?
 
Shaikh Ali
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

yusuf Kaplan wrote:

Thorsten Schneider wrote:With the third assignment the compiler only sees byte d = an int + an int


I think the compiler sees not an int + an int but rather a byte + a byte. But as with Sierra & Bates the result of this expression is an int, right?
Anyway, I think I got the problem. The compiler can not assure that the result of this expression is less then 128. If it could, it would compile. Is that true?



You got it. Try to compile this:


 
Get out of my mind! Look! A tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic