码迷,mamicode.com
首页 > 其他好文 > 详细

赋值运算符

时间:2019-04-13 15:11:39      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:col   编译   void   分析   等价   mic   问题   pack   nbsp   

赋值运算符的种类

技术图片

代码演示:

package com.wu.day02;

public class Demo7Operator {
    public static void main(String[] args) {
        int i = 6;
        // i +=5 等价于 i = i+5;
        i += 5;
        System.out.println(i);//11
        // i -= 5等价于 i = i-5
        i -= 5;
        System.out.println(i);//6
        // i *= 5等价于 i = i*5
        i *= 5;
        System.out.println(i);//30
        // i /= 5等价于 i = i/5
        i /= 5;//6
        System.out.println(i);
        // i %= 5等价于 i = i%5
        i %= 5;
        System.out.println(i);//1

    }
}

注意事项:

  • 只要变量才能使用赋值运算符,常量不可以
  • 复合赋值运算符其中隐含了一个强制类型转换
        short s = 1;
        /*
        * 分析: s += 1 逻辑上看作是 s = s + 1 计算结果被提升为int类型,再向short类型赋值时发生错误,因为不能将取值范围
          大的类型赋值到取值范围小的类型。但是, s=s+1进行两次运算 , += 是一个运算符,只运算一次,并带有强制转换的特点,
         也就是说 s += 1 就是 s = (short)(s + 1) ,因此程序没有问题编译通过,运行结果是2.

        * */
        s += 1;
        System.out.println(s);//2

 

赋值运算符

标签:col   编译   void   分析   等价   mic   问题   pack   nbsp   

原文地址:https://www.cnblogs.com/wurengen/p/10701154.html

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