• 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

clear the doubt please

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1 public class Q5
2 {
3 public static void main(String rag[])
4 {
5 byte b=1;
6 b++;
7 byte b=127+1;
8 int i=2147483647+1;
9 b=- -b;
10 }
11 }
Will line no 8 causes error or not ?
Why?
Thanks,
Sankar
 
Ranch Hand
Posts: 1140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by amrita sankar:
Will line no 8 causes error or not ? Why?



Did you try it? What happens? Does it give any error message? What does the error reads?
 
amrita sankar
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
line 7 and 9 gives error

not 8 but int range is upto 2^32 -1 only
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi amrita,

The answer is yes, the code compiles fine but the results is not as expected.

Once int variables can hold 31 bits plus one for sign, ints can accept values from -2^31 ~ 2^31 - 1. So all values between -21474883648 and 21474883647 are supposed to fit in an int variable.

However, when you try to assign the value 21474883647 + 1, the left most bits are discarded to fit this new number.

So, if you check the value for i variable after line 8 it is -2147483648.

What about if you change your code to 21474883647 + 2 ? The value for variable now is -2147483647.

If you change to 21474883647 + 3, its value is -21474883646 and so on ...
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm sorry, my explanation refers only to line #8. I'm not considering the line #7.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think error is ther on page no 7 bcoz byte range is +127 only.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic