i++和i--运算符优先级

1、问题背景

/**
 * 測试i++和i--
 */
package com.you.model;

/**
 * @author YouHaiDong
 * @date 2014-08-16
 */
@SuppressWarnings("unused")
public class AddReduce 
{
	static
	{
		int x = 10;
	}
	
	static int x;
	static int y;
	
	public static void addReduce()
	{
		y = x++ + ++x;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) 
	{
		x--;
		addReduce();
        System.out.println(x + y++ + x);
	}

}

2、问题分析

(1)这里的x是一个局部变量,仅仅对这里有影响

static
{
    int x = 10;
}
(2)初始化时,x=0,y=0
static int x;
static int y;

(3)因为初始化时x=0,运行x--,x就变为-1

x--;

(4)在调用“addReduce();”方法时,y=0,x=1

public static void addReduce()
{
	y = x++ + ++x;
}
(5)x + y++ + x = 1 + 0 + 1 = 2

System.out.println(x + y++ + x);


3、分析结果

   由2分析出,该代码执行的结果为:2

原文地址:https://www.cnblogs.com/zfyouxi/p/4305694.html