906
技術社區[雲棲]
Java中關於i++與++i的問題
在《Java程序員麵試寶典》裏麵有提到i++這個部分,j++是一個依賴於java裏麵的“中間緩存變量機製”來實現的。通俗的說
++在前就是“先加後賦”(++j)
++在後就是“先賦後加”(j++)
package cn.xy.test; public class TestPlus { private static int a() { int i = 10; int a = 0; a = i++ + i++; // temp1 = i; 10 // i = i + 1; 11 // temp2 = i; 11 // i = i + 1; 12 // a = temp1 + temp2 = 21; return a; } private static int b() { int i = 10; int b = 0; b = ++i + ++i; // i = i + 1; 11 // temp1 = i; 11 // i = i + 1; 12 // temp2 = i; 12 // b = temp1 + temp2 = 23; return b; } private static int c() { int i = 10; int c = 0; c = ++i + i++; // i = i + 1; 11 // temp1 = i; 11 // temp2 = i 11 // i = i + 1; 12 // c = temp1 + temp2 = 22 return c; } private static int d() { int i = 10; int d = 0; d = i++ + ++i; // temp1 = i; 10 // i = i + 1; 11 // i = i + 1; 12 // temp2 = i; 12 // d = temp1 + temp2 = 22; return d; } public static void main(String[] args) { System.out.println(a()); System.out.println(b()); System.out.println(c()); System.out.println(d()); } }
原帖地址:https://blog.csdn.net/zlqqhs/article/details/8288800
最後更新:2017-04-04 07:32:14