Byte a = 123;
a++;
System.out.println(a);// 124
/*
* 上面的結(jié)論是: a++的時(shí)候首先是 創(chuàng)建一個(gè)Byte對(duì)象124,然后將a引用指向這個(gè)Byte 124對(duì)象,這個(gè)解釋有問(wèn)題嗎?
*/
Byte b = 126;
b = b++;
System.out.println(b);// 126
/*
* 根據(jù)第一段代碼的執(zhí)行結(jié)果;
* b=b++,的執(zhí)行操作是首相將b引用賦值給b引用,所以沒有改變,然后將b引用所指向的對(duì)象自增(這個(gè)實(shí)現(xiàn)的過(guò)程是創(chuàng)建一個(gè)對(duì)象值為127,
* 然后讓b引用指向這個(gè)127),如果這樣理解,結(jié)果不應(yīng)該是127嗎
*/
求解,謝謝
學(xué)習(xí)是最好的投資!
b = b++: If you know C++, you can refer to the auto-increment implementation of ++, and you should be able to understand why it is 126
Byte operator++(Byte) {
Byte temp = *this;
this->value = this->value + 1;
return temp;
}
can be understood as b is equal to temp before the operation is incremented
++, --Be sure to write it separately, otherwise you will cause trouble for yourself.
For questions like i=i+++++i
, I can only despise...
Java stack frame contains local variable table and operand stack. The ++ auto-increment operation is direct operation on the value in the local variable table. When i=i++, first push the i in the local variable table into the operand stack. Then add 1 to i in the local variable table to become 127, and then write i (126) in the operand stack back to i in the local variable table, covering the 127 data and changing it to 126. The order of ++i is different. It first increments i in the local variable table and then adds it to the operand stack.