码迷,mamicode.com
首页 > 编程语言 > 详细

Java一元操作符++详解

时间:2016-11-13 22:16:19      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:默认   pre   class   赋值   any   赋值操作符   copyright   style   java   

废话不多说,直接上代码。

package com.coshaho.learn;

/**
 * 
 * OperatorLearn.java Create on 2016-11-13 下午8:38:15    
 *    
 * 类功能说明: 深入理解++操作符  
 *
 * Copyright: Copyright(c) 2013 
 * Company: COSHAHO
 * @Version 1.0
 * @Author coshaho
 */
public class OperatorLearn 
{
    // 一元操作符,赋值操作符,三目操作符从右向左运算,其他操作符从左向右运算
    // ++x步骤:1.返回x+1;2.执行x = x + 1;
    // x++步骤:1.返回x;2.执行x = x + 1;
    public static void main(String[] args)
    {
         int x = 1;
         // 步骤1. 计算y(默认值0);
         // 步骤2. (x++)返回x值给临时变量c,为1;
         // 步骤3. x = x + 1,x为2;
         // 步骤4. 计算x,x为2;
         // 步骤5. y = 临时变量c + 2 = 1 + 2 = 3.
         int y = (x++) + x;
         System.out.println("x = " + x);
         System.out.println("y = " + y);
         
         x = 1;
         // 步骤1. 计算y(默认值0);
         // 步骤2. (++x)返回x+1值给临时变量c,为2;
         // 步骤3. x = x + 1,x为2;
         // 步骤4. 计算x,x为2;
         // 步骤5. y = 临时变量c + 2 = 2 + 2 = 4.
         y = (++x) + x;
         System.out.println("x = " + x);
         System.out.println("y = " + y);
         
         x = 1;
         // 步骤1. 计算y(默认值0);
         // 步骤2. 计算x,为1;
         // 步骤3. (++x)返回x+1值给临时变量c,为2;
         // 步骤4. x = x + 1,x为2;
         // 步骤5. y = 1 + c = 1 + 2 = 3.
         y = x + (++x);
         System.out.println("x = " + x);
         System.out.println("y = " + y);
         
         x = 1;
         // 步骤1. 计算y(默认值0);
         // 步骤2. 计算x,为1;
         // 步骤3. (x++)返回x值给临时变量c,为1;
         // 步骤4. x = x + 1,x为2;
         // 步骤5. y = 1 + c = 1 + 1 = 2.
         y = x + (x++);
         System.out.println("x = " + x);
         System.out.println("y = " + y);
         
         x = 1;
         // 1.计算x,为1;
         // 2.计算(x++)返回1给临时变量c
         // 3.x = x + 1,为2;
         // 4.计算x = 1 + c = 1 + 1 = 2;
         x += (x++);
         System.out.println("x = " + x);
         
         int[] xx = {1,3};
         int i = 0;
         xx[i++] *= 2;
         System.out.println("xx[0] = " + xx[0] + ", xx[1] = " + xx[1]);
         
         xx = new int[]{1,3};
         i = 0;
         xx[i++] = xx[i++] * 2;
         System.out.println("xx[0] = " + xx[0] + ", xx[1] = " + xx[1]);
         
         /**
          * 输出
          * x = 2
            y = 3
            x = 2
            y = 4
            x = 2
            y = 3
            x = 2
            y = 2
            x = 2
            xx[0] = 2, xx[1] = 3
            xx[0] = 6, xx[1] = 3
          */
    }
}

 

Java一元操作符++详解

标签:默认   pre   class   赋值   any   赋值操作符   copyright   style   java   

原文地址:http://www.cnblogs.com/coshaho/p/6059900.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!