0

How does byte work in this code

Java data types https://code.sololearn.com/cvr4lL627XXz/?ref=app

14th Jun 2021, 12:39 PM
alagammai uma
alagammai uma - avatar
6 Réponses
+ 3
It is true that the byte ranges from -128 to 127. But it is going nowhere close to infinity. This is called overflowing, when 'by' reaches 127 it is greater than 0. So the loop continues and 'by' is incremented by 1. Now 'by' becomes -128, yes 127+1=-128. So -128 is less than 0 and the loop breaks. The value of 'by' is then printed. Print the value of 'by' inside the loop before incrementing to see how it works.
14th Jun 2021, 2:45 PM
Avinesh
Avinesh - avatar
+ 3
Do you know bytes range from -128 to 127. So your code is going infinity after reaching 127 it again goes to -128 which is smaller than 0
14th Jun 2021, 12:45 PM
Atul [Inactive]
+ 2
My pleasure alagammai uma
14th Jun 2021, 1:00 PM
Atul [Inactive]
+ 1
Avinesh thank u
14th Jun 2021, 4:25 PM
alagammai uma
alagammai uma - avatar
+ 1
alagammai uma You're welcome
14th Jun 2021, 4:36 PM
Avinesh
Avinesh - avatar
0
Atul thanks
14th Jun 2021, 12:54 PM
alagammai uma
alagammai uma - avatar