zoukankan      html  css  js  c++  java
  • 赋值运算符

    +=    左+右  赋给  左
    -=    左-右  赋给  左
    *=    左*右  赋给  左
    /=    左*右  赋给  左
    %=    左%右  赋给  左

    int x=6;

    x+=3;   //9    x=x+3
    x-=2;   //4    x=x-2
    x*=4;   //24    x=x*4
    x/=2;   //3    x=x/3
    x%=4;  //2    x=x%3

    注:

    赋值运算符,是一次运算。计算过程隐含了强制类型转换动作。计算结果与左侧类型一致。

    short x=6;
    x=x+1;

    //报错。两次计算,先相加,再赋赋值。精度损失。

    short x=6;
    x += 1;
    //输出 7。一次计算,直接赋值。+=后的x仍然是short类型。s += 1,等价于 s = (s的数据类型)(s + 1)。

    示例:

    class Demo2
    {
        public static void main(String[] args)
        {
            float x=6;
            x+=1;  
            System.out.println(x);
        }
    }

    输出结果:7.0。

    结果是float类型。

    class Demo2
    {
        public static void main(String[] args)
        {
            int x=6;
            x+=2.5;  
            System.out.println(x);
        }
    }

    输出结果:8。

    x=8.5,int型,取8。

  • 相关阅读:
    Shiro认证过程?
    使用过Redis做异步队列么,你是怎么用的?
    使用 Spring Boot有什么好处
    Spring Boot、Spring MVC 和 Spring 有什么区别?
    es
    python并发编程
    Go基础05
    Go04基础
    Go03基础
    Go基础02
  • 原文地址:https://www.cnblogs.com/ibelieve618/p/6370332.html
Copyright © 2011-2022 走看看