1 public class Test2 {
2 public void add(Byte b) {
3 b = b++;
4 }
5
6 public void test() {
7 Byte a = 127;
8 Byte b = 127;
9 add(++a);
10 System.out.println("a = "+a);
11 add(b);
12 System.out.println("b = "+b);
13 }
14
15 public static void main(String[] args) {
16 Test2 test2=new Test2();
17 test2.test();
18 }
19 }
運行結果:
a = -128
b = 127
分析:首先byte的范圍為-128~127。字節長度為8位,最左邊的是符號位,而127的二進制為:0111 1111,所以執行++a時,0111 111變為1000 0000,而128的二進制為:1000 0000,即為127+1=-128;而add(b)其實為add(127),而b=b++其實為b=127,b++;則b=127。
