3

A simple line of code:

String thing = "Something";
thing += " something else" + " and more.";

IntelliJ IDEA offers to change this line into 4 other ways to accomplish the same result:

String.format()
StringBuilder.append()
java.text.MessageFormat.format()
Replace += with =

Why? What is so wrong with +=? Can anyone explain this please? Thanks.

Jeex
  • 127
  • 8
  • These suggestions are called Intention Actions. They can be found / enabled / disabled in **File | Settings | Editor | Intentions** (see https://www.jetbrains.com/help/idea/intention-actions.html) – Koyasha Mar 04 '20 at 12:01
  • Title is vague. Rewrite to summarize your specific technical issue. – Basil Bourque Mar 04 '20 at 14:57

2 Answers2

2

In general case, the result is not the same. + operator allocates intermediate string, which requires additional memory.

String thing = "Something";
thing += " something else" + " and more."; // two allocations: to compute string inside expression ```" something else" + " and more."```` and to execute ```+=```

By using StringBuilder you don't allocate intermediate result, e.g. less memory is needed.

In your case with short lines and without loops no actual performance boost is expected. However with loops you receive O(N^2) complexity. Please check this answer with explained example.

Manushin Igor
  • 3,398
  • 1
  • 26
  • 40
  • 1
    On paper it is true (might be useful to know). In practice it doesn't matter because compiler will optimize it anyway and use StringBuilder instead of direct concatenation. For reference: https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.18.1 (the text in the smaller font). – Amongalen Mar 04 '20 at 11:34
  • @Amongalen, yes. compiler can optimize this (theoretically for some versions). And as I said, this code doesn't need any ```StringBuilder```, because there aren't loops here. – Manushin Igor Mar 04 '20 at 13:13
  • 1
    Thanks @ManushinIgor. The actual code contains over 1000 lines and many of these remarks are placed by IntelliJ. The main task is generating long lines of text, so StringBuilder would be useful to optimize the code. Thanks. – Jeex Mar 04 '20 at 15:37
  • Concatenation of two string literals will be resolved compile time to a single string constant, so the only real issue here is the `+=` – Mark Rotteveel Mar 04 '20 at 19:23
2

I am assuming you are referring to this:

enter image description here

This is not actually a fix "problem" with your code, as indicated by the pencil icon. Fixes to problems with your code are identified with a lightbulb, such as not using the value of thing:

enter image description here

The pencil just means "here's a shortcut to change your code, so you don't have to change it manually". Changing a a += b to a = a + b usually isn't that much work, but other things, like changing a for-each loop to a regular for loop, is.

enter image description here

This could be useful if you suddenly remembered that you need the index of the array for something.

Sweeper
  • 213,210
  • 22
  • 193
  • 313
  • These 'shortcuts' are called Intention Actions, which can be found / enabled / disabled in *File | Settings | Editor | Intentions* (https://www.jetbrains.com/help/idea/intention-actions.html) – Koyasha Mar 04 '20 at 11:59
  • Thanks for the reply, @Sweeper. I was just wondering if there are good reasons to use the one or the other method. But as you explained: that is not what these remarks by IntelliJ are meant for. Thanks. – Jeex Mar 04 '20 at 15:33
  • B.t.w. These IntelliJ remarks really are educational. – Jeex Mar 04 '20 at 16:09