TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Java: "+=" applied to String operands can provoke side effects

240 点作者 javinpaul将近 7 年前

8 条评论

kjeetgill将近 7 年前
Wow. This is a pretty big bug. The official bug report: <a href="https:&#x2F;&#x2F;bugs.openjdk.java.net&#x2F;browse&#x2F;JDK-8204322" rel="nofollow">https:&#x2F;&#x2F;bugs.openjdk.java.net&#x2F;browse&#x2F;JDK-8204322</a> as Stuart Marks, a jvm architect, posted. I&#x27;m surprised it stayed uncaught for so long.<p>For the unfamiliar, the way string concatenation is compiled changed with invokedynamic.<p>Previously, it would compile to either Sting.concat calls or transform into a StringBuilder, but with invoke dynamic we can leave it to the runtime to optimize when it first runs.
评论 #17249019 未加载
评论 #17249033 未加载
评论 #17250017 未加载
评论 #17248989 未加载
评论 #17249410 未加载
Terr_将近 7 年前
&gt; Operator += is broken for Strings in Java 9 and 10<p>The title [Edit: since changed] is a little bit fear-monger-y. It&#x27;s only broken when the left hand side is not safe to evaluate more than once.<p>It can be prevented with a compile-time flag.
评论 #17248667 未加载
评论 #17248599 未加载
评论 #17249015 未加载
评论 #17248675 未加载
评论 #17249300 未加载
评论 #17250084 未加载
nine_k将近 7 年前
Adding side effects to your array index expression and depending on order of their evaluation is a bad idea anyway.<p>Unfortunately, the `++` &#x2F; `--` made it to Java, and are still used sometimes, despite JVM not being PDP-11, and definitely not trying to conserve every precious byte of RAM.
评论 #17248704 未加载
评论 #17249145 未加载
评论 #17249039 未加载
评论 #17248763 未加载
freefal将近 7 年前
I used that operator in a tight loop before the compiler was smart enough to automatically convert the code to use a StringBuilder. Boy was that slow...
评论 #17251416 未加载
ianamartin将近 7 年前
Time to rewrite Java in Rust.
评论 #17248986 未加载
评论 #17249006 未加载
kazinator将近 7 年前
Lisp macros get this right.<p><pre><code> [2]&gt; (macroexpand &#x27;(incf (aref a (incf i)) x)) (LET* ((#:G3214 A) (#:G3215 (INCF I)) (#:G3217 X) (#:G3216 (+ (AREF #:G3214 #:G3215) #:G3217))) (SYSTEM::STORE #:G3214 #:G3215 #:G3216)) ; T </code></pre> You can&#x27;t just blindly expand the += by copying the expressions&#x2F;AST node on the left. Or in general; you don&#x27;t take expressions in the program and blindly duplicate them. How can someone working on compilers not know that???
评论 #17249956 未加载
pleasecalllater将近 7 年前
Yea... what was the Bloch&#x27;s explanation for not having operator overloading in Java? I think it was something like &quot;it&#x27;s not allowed because I&#x27;ve seen too many programmers who did that in a wrong way&quot; :)<p>On the other hand this explanation is stupid, as a programmer can implement `add()` in a wrong way.
oblong将近 7 年前
&gt; &quot;side effects&quot;<p>A side effect would be like submitting an order for am Amazon Echo by mistake.<p>This is a bug, a serious bug, that leads to variables being assigned the wrong value.
评论 #17251828 未加载