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

java – a = b和a = a b之间的区别

发布时间:2020-12-15 02:06:09 所属栏目:Java 来源:网络整理
导读:在 Java中,我真的很想知道使用a = b之间是否存在差异;或a = a b; .我应该主要使用哪一个?我知道第一个是快捷方式,但编译器是否会以不同方式获得这两个指示? 解决方法 见 Java language specification,15.26.2 Compound assignment operators 引用相关部分
在 Java中,我真的很想知道使用a = b之间是否存在差异;或a = a b; .我应该主要使用哪一个?我知道第一个是快捷方式,但编译器是否会以不同方式获得这两个指示?

解决方法

见 Java language specification,15.26.2 Compound assignment operators

引用相关部分:

A compound assignment expression of the form E1 op= E2 is equivalent
to E1 = (T)((E1) op (E2)),where T is the type of E1,except that E1
is evaluated only once.

short x = 3;
x += 4.6;

and results in x having the value 7 because it is equivalent to:

short x = 3;
x = (short)(x + 4.6);

因此它不仅仅是语法糖

int x = 1;
long a = 2l;
x+= a;

编译,在哪里

int x =1;
long a =2l;
x = x+a;

给出了编译错误,如here on StackOverflow quite recently所述

(编辑:李大同)

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

    推荐文章
      热点阅读