+ 2

BagJava

Please can who explain code below Why output - 128 byte b1 = 1; while(b>0) by++; System.out.println(by);

12th Dec 2017, 6:06 AM
Стрельбицкий Мирослав
Стрельбицкий Мирослав - avatar
2 Answers
+ 5
Type range overflow. A byte has a range of -128 to 127. When you exceed the range it will wrap around to the opposite end of the range by x amount remaining in the mathematical operation. I.E. 127 + 1 = -128 -128 - 1 = 127 125 + 5 = -126 etc
12th Dec 2017, 6:16 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Byte can held numbers up to 128. After it full it flips the sign. So you get kind of value overflowing.
12th Dec 2017, 6:15 AM
Dima Makieiev
Dima Makieiev - avatar