• 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

Byte

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
byte c = a + b;

byte a = 3;
byte b = 8;

why the line 1 won't compile?
 
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sumaraghavi,

it doesn't compile because if you add two bytes the result in general doesn't fit into a single byte. This could easily produce an overflow so the compiler doesn't allow it.

You have to keep in mind, that the compiler ignores the values used in your code. For you it's obvious that 3+8 fits in one byte but the compiler simply doesn't know this!

Marco
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is because the compiler internally treats the literals as int and sum of two literals will again be int.Hence to convert the result of type int to byte you need to specify explcitly ie explicit cast is required.
 
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

it doesn't compile because if you add two bytes the result in general doesn't fit into a single byte. This could easily produce an overflow so the compiler doesn't allow it.

You have to keep in mind, that the compiler ignores the values used in your code. For you it's obvious that 3+8 fits in one byte but the compiler simply doesn't know this!



It doesn't compile because of data type promotion.

Before doing addition of two bytes the byte is promoted to int. and then two integer(a,b) adds up. Now we know integer can't fit into byte. Though we know that 8 fits into byte, compiler doesn't.

To fix it you can,

1) Make a and b final.


Now, compiler is aware about the values of a and b.And also knows that they are not gonna altered.So it's not harmful to me anymore.let them allow.

2) Do casting to byte.

Here, it will caste an integer to byte and see if it fits into byte or not.If it doesn't fit then you might not get expected answer.
[ June 11, 2008: Message edited by: Vishal Pandya ]
 
Marco Ehrentreich
best scout
Posts: 1294
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

You have to keep in mind, that the compiler ignores the values used in your code. For you it's obvious that 3+8 fits in one byte but the compiler simply doesn't know this!


OK, this should have been a simple explanation. But as I see it was to imprecise. Of course the compiler does know the values but it's like it would pretend it doesn't know if the result really fits in one byte (because of type promotion to int). Sorry for phrasing this not so well... Not a native speaker :roll:
 
Vikas Kapoor
Ranch Hand
Posts: 1374
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sorry for phrasing this not so well... Not a native speaker


Me too.
No worry.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vishal Pandya:
It doesn't compile because of data type promotion.

Before doing addition of two bytes the byte is promoted to int. and then two integer(a,b) adds up. Now we know integer can't fit into byte. Though we know that 8 fits into byte, compiler doesn't.


Indeed. The result type of any numeric calculation is at least an int. The rule is as follows (in order):
- if any of the operands is double, the result is double
- if any of the operands is float, the result is float
- if any of the operands is long, the result is long
- otherwise the result is int
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by sumaraghavi ragha:
byte c = a + b;

byte a = 3;
byte b = 8;

why the line 1 won't compile?



If this is your code, this actually won't compile because on line 1, a and b don't exist. The compiler throws "cannot find symbol" errors.

I know you PROBABLY meant to have your code look like this:


But please remember, it it MUCH harder to answer your question when we have to GUESS what you meant. Further, it is also a BIG HELP if you post the EXACT error message you get. If you had posted this:


Then we really WOULD know what the problem was.

These are just some tips for the next time you post.
 
sumaraghavi ragha
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all for your explanation

Thanks for helping minds
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic