0

I've heard (can't find the source) that starting on Java 5 statements such as

String a = "a" + "B" + "c"; were actually optimized by the compiler into String a = "aBc";

But I can't find it online.

Can you help a sister out?

Could you please reconsider marking my question as a duplicate? The question marked as dup actually is the behavior of the compiler, NOT the reference to the docs, which is my question about.

Also, my question asks when this behavior started, which is not addressed by the question marked as dup.

Cindy Langdon
  • 611
  • 6
  • 12
  • You want us to find the source of that statement? – Sotirios Delimanolis Jul 09 '14 at 17:34
  • Just for the record, I am not being lazy. I did spend a considerable amount of time and there are some references on the web, but I can't find a good source, such as a JSR doc, or an Oracle page. – Cindy Langdon Jul 09 '14 at 17:36
  • 3
    @CindyLangdon: Did you try the language specification, and look for "constant"? Note that this was not new in Java 5. I believe it's been the case forever. – Jon Skeet Jul 09 '14 at 17:37
  • @sᴜʀᴇsʜᴀᴛᴛᴀ: That's not what the question is about though. It's about *compile-time* concatenation of constant strings. – Jon Skeet Jul 09 '14 at 17:37
  • Yes. I saw that, @sᴜʀᴇsʜᴀᴛᴛᴀ, but there is also a native component for Strings. Also, the behavior I mentioned is an optimization for the VM, not of the source code. – Cindy Langdon Jul 09 '14 at 17:38
  • 1
    http://stackoverflow.com/questions/8581824/does-java-compiler-include-string-constant-folding, http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html#15.28 – nneonneo Jul 09 '14 at 17:38
  • 1
    @CindyLangdon Actually, it's a source code transformation. The VM has no clue that `a` was formed by the concatenation of several strings. It's all taken care of by the compiler. Unless that's what you meant by "optimization for the VM"? – awksp Jul 09 '14 at 17:40
  • I think the answer provided by @nneonneo is good enough. Would you like to post it? The second reference. The first one is not good. – Cindy Langdon Jul 09 '14 at 17:43
  • @CindyLangdon: I would but this question is already marked as a duplicate. – nneonneo Jul 09 '14 at 17:43
  • I believe it's answered by http://stackoverflow.com/a/24660640/376366, with reference to the documentation. – Fernando Correia Jul 09 '14 at 18:14
  • @FernandoCorreia That is a reference to the doc. Not saying when that behavior started. – Cindy Langdon Jul 09 '14 at 18:24

0 Answers0