zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Mysql 备份 导入导出
    简 历
    Mysql 表结构 创建 限制 关联
    Unity 碰撞检测
    Unity 获取键值
    关于大型网站系统的一些问题
    关于zookeeper
    dubbo分布式和消息队列
    集群
    cookie及安全问题
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4305694.html
Copyright © 2011-2022 走看看