首页 > 其他 > 详细

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

时间:2014-08-16 15:07:20      阅读:385      评论:0      收藏:0      [点我收藏+]

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

i++和i--运算符优先级,布布扣,bubuko.com

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

原文:http://blog.csdn.net/you23hai45/article/details/38613453

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!