加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

我有以下Java代码的问题

发布时间:2020-12-15 05:16:12 所属栏目:Java 来源:网络整理
导读:public class b { public static void main(String[] args) { byte b = 1; long l = 127; // b = b + l; // 1 if I try this then it does not compile b += l; // 2 if I try this then it does compile System.out.println(b); }} 我正在使用此代码,但我有
public class b {
    public static void main(String[] args) {
        byte b = 1;
        long l = 127;
    //  b = b + l;            // 1 if I try this then it does not compile
        b += l;               // 2 if I try this then it does     compile
        System.out.println(b);  
    }
}

我正在使用此代码,但我有问题:
我不明白为什么b = b l;没有编译,但如果我写b = l;然后它编译并运行.

请解释为什么会这样.

解决方法

这就是复合赋值运算符(如=,– =等)优于赋值运算符的优点,您必须明确地转换为右侧的类型,但如果使用复合赋值运算符,它会隐式为您执行此操作. 正如你的情况一样.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读