Unexpected endless byte for loop
问题 I have the following loop: for (byte i = 0 ; i < 128; i++) { System.out.println(i + 1 + " " + name); } When I execute my programm it prints all numbers from -128 to 127 in an infinite loop. Why does this happen? 回答1: byte is a 1-byte type so can vary between -128...127, so condition i < 128 is always true. When you add 1 to 127 it overflows and becomes -128 and so on in a (infinite) loop... 回答2: After 127, when it increments, it will become -128, so your condition won't match . byte : The