Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 98f7c44

Browse files
committed
Java += 操作符实质
1 parent 0909044 commit 98f7c44

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ stackoverflow-Java-top-qa
99
> 基础语法
1010
1111
* [Java是按值传递还是按引用传递](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/is-java-pass-by-reference-or-pass-by-value.md)
12+
* [Java += 操作符实质](https://github.com/giantray/stackoverflow-java-top-qa/blob/master/contents/java-operator.md)
13+
1214

1315

1416
> 代码规范
@@ -26,8 +28,6 @@ stackoverflow-Java-top-qa
2628
### 待翻译问题链接(还剩0问题)
2729
- [Why is processing a sorted array faster than an unsorted array?](http://stackoverflow.com/questions/11227809/why-is-processing-a-sorted-array-faster-than-an-unsorted-array)
2830
- [Why is subtracting these two times (in 1927) giving a strange result?](http://stackoverflow.com/questions/6841333/why-is-subtracting-these-two-times-in-1927-giving-a-strange-result)
29-
- [Is Java "pass-by-reference" or "pass-by-value"?](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value)
30-
- [Java += operator](http://stackoverflow.com/questions/8710619/java-operator)
3131
- [Avoiding "!= null" statements in Java?](http://stackoverflow.com/questions/271526/avoiding-null-statements-in-java)
3232
- [Proper use cases for Android UserManager.isUserAGoat()?](http://stackoverflow.com/questions/13375357/proper-use-cases-for-android-usermanager-isuseragoat)
3333
- [Differences between HashMap and Hashtable?](http://stackoverflow.com/questions/40471/differences-between-hashmap-and-hashtable)

‎contents/java-operator.md‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
##Java += 操作符实质
2+
3+
###问题
4+
直到今天,我都一直以为:
5+
i += j 等同于 i = i + j;
6+
但假设有:
7+
int i = 5;
8+
long j = 8;
9+
这时 i = i + j不能编译,但i += j却可以编译。这说明两者还是有差别的
10+
这是否意味着,i += j,实际是等同于 i= (type of i) (i + j)呢?
11+
12+
###回答
13+
这个问题,其实官方文档中已经解答了。 请看这里。[§15.26.2 Compound Assignment Operators](http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html#15.26.2 )
14+
再照搬下官方文档的说明
15+
对复合赋值表达式来说,E1 op= E2(诸如i += j;i-=j等等),其实是等同于E1 = (T)((E1) op (E2)),其中,T是E1这个元素的类型。
16+
举例来说,如下的代码
17+
short x = 3;
18+
x += 4.6;
19+
等于
20+
short x = 3;
21+
x = (short)(x + 4.6);
22+
23+
24+
stackoverflow链接
25+
http://stackoverflow.com/questions/8710619/java-operator

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /