Note that there are some explanatory texts on larger screens.

plurals
  1. POJava multiline string
    primarykey
    data
    text
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. COStringBuilder.append() is preferable to plus when repeatedly adding to a string because every time you do `string1 + string2` you're allocating a new string object and copying the characters from both of the input strings. If you're adding n Strings together you'd be doing n-1 allocations and approximately (n^2)/2 character copies. StringBuilder, on the other hand, copies and reallocates less frequently (though it still does both when you exceed the size of its internal buffer). Theoretically, there are cases where the compiler could convert + to use StringBuilder but in practice who knows.
      singulars
    2. COOops. That part abut the character copies was a bit confused/unclear. Each character gets copied once for every string that comes after it in the concatenation. If all of your strings are one character you get roughly (n^2)/2 character copies. It's really sort of (n^2)/2 string copies, except the later times a string is copied it's copied along with the stuff it's been joined to (but the cost is proprotional to the number of characters, so counting string copies is a bit misleading). Long story short: the cost is roughly quadratic, while StringBuilder tries to make the amortized cost linear.
      singulars
    3. COThere's an article on this topic -- I've added a link below, but here it is again: http://java.sun.com/developer/JDCTechTips/2002/tt0305.html
      singulars
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload