JLS: Clarify that assignment result does not re-read variable

some-java-user-99206970363698485155 at vodafonemail.de some-java-user-99206970363698485155 at vodafonemail.de
Wed Apr 2 19:58:47 UTC 2025


Hello,

the JLS 23 § 15.26 "Assignment Operators" [1] says this about the result 
of assignments:

> Thus, |a=b=c| means |a=(b=c)|, which assigns the value of |c| to |b| 
> and then assigns the value of |b| to |a|. 
> At run time, the result of the assignment expression is the value of 
> the variable after the assignment has occurred.

This creates some ambiguity because these sentences sound as if there is 
(or at least there is permitted to be) an additional read of the 
assigned variable. That is, |a=b=c| is equivalent to:
```
b = c
a = b
```

However, what is actually happening is (if I understand it correctly):
```
temp = c
b = temp
a = temp
```
(respectively the compiler just emits a `dup` before assigning the value 
to the variables)

This does make a difference for concurrent programs where a racy write 
is (intentionally) happening to the intermediate variable, in the 
example above 'b'.

Therefore it would probably be good to reword that JLS section, or 
clarify that the assigned variable is not read again for the result 
value (unless JVMs are actually permitted to do that).


[1] https://docs.oracle.com/javase/specs/jls/se23/html/jls-15.html#jls-15.26
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/jls-jvms-spec-comments/attachments/20250402/e5888bed/attachment.htm>


More information about the jls-jvms-spec-comments mailing list