• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

byte

 
Sireesha Mullapudi
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,In the following code



int k=2147483647+1;
System.out.println(k);
byte bt=127+1;
System.out.println(bt);



why int is not giving compile time error?
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because the type of the result of the addition is an int.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Lakshmi,


in the byte case, you perform an addition.
Because of the plus, both bytes are converted to ints automatically (language specification, 5.6.2 Binary Numeric Promotion) and the result computed at compile time. The compiler recognizes that the result (128 I think, didn't check it on a calculator) is too big for a byte, so a compiler error occurs.

With the ints, everything stays an int, so the compiler does not know, that an overflow will occur. The calculation is made internally with an int, so the compiler cannot know, that it is "too big" - internally the value cannot be too big, due to overflow, it just adds up to minus 2147483648.
And that fits into the variable k.


Yours,
Bu.
 
Sireesha Mullapudi
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic