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

  • 相关阅读:
    runtime iOS 运行时机制
    iOS 文件操作
    responseCode 状态吗查询
    iOS常用宏定义
    Block里用self造成循环引用
    iOS Block全面分析
    OC与Swift混编
    iOS打包app发给测试人员测试
    Swift UITextField
    sqilite学习
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13314856.html
Copyright © 2011-2022 走看看