閱讀698 返回首頁    go 阿裏雲 go 技術社區[雲棲]


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-03 07:57:30

  上一篇:go 管理中第一可怕之事(2) .
  下一篇:go Android開發9——Activity的啟動模式