Timeline for String concatenation: concat() vs "+" operator
Current License: CC BY-SA 3.0
27 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Aug 15, 2023 at 17:57 | comment | added | Louis Wasserman |
Things have changed since this answer was updated. I don't believe there's any scenario remaining in which StringBuilder or concat beat + due to JEP 280.
|
|
| Jul 11, 2020 at 4:57 | comment | added | NanoNova | Could u elaborate more about "bytecode compiler cheats" part? | |
| Oct 12, 2017 at 12:57 | history | edited | Tom Hawtin - tackline | CC BY-SA 3.0 |
added 598 characters in body
|
| Oct 6, 2017 at 13:25 | comment | added | Paweł Adamski | Things have changed since when this answer was created. Please read my answer bellow. | |
| Oct 28, 2015 at 15:01 | history | edited | Tom Hawtin - tackline | CC BY-SA 3.0 |
I guess we're assuming people are newbie enough to not know what NPE is... on SO.
|
| Oct 28, 2015 at 15:00 | history | rollback | Tom Hawtin - tackline |
Rollback to Revision 6
|
|
| S Oct 28, 2015 at 6:09 | history | edited | shareef | CC BY-SA 3.0 |
expand NPE abbreviation for readability
|
| S Oct 28, 2015 at 6:09 | history | suggested | Gerald | CC BY-SA 3.0 |
expand NPE abbreviation for readability
|
| Oct 28, 2015 at 5:35 | review | Suggested edits | |||
| S Oct 28, 2015 at 6:09 | |||||
| May 21, 2015 at 15:53 | comment | added | EliuX | About that StringBuilder is better than concat: Correct me if i am wrong, but StringBuilder, and others of his kind allocates 2x the memory for receiving new content that exceeds the capacity need. Building SQL querys i got several times an error, for this reason in tests, and i solved using ´concat´, was a little bit les confortable but the memory was good. | |
| Apr 4, 2015 at 21:05 | comment | added | most venerable sir | So int = short + short will not compiled. But b/c the shorthand cast the int result to short. In the format short+=short, the same expression will compile. Am I understanding it right? | |
| Apr 4, 2015 at 20:51 | comment | added | Tom Hawtin - tackline |
@user132522 For types such as short the result of + would be int (because that's the sort of weird thing Java does). So a = a + b; would not compile as you can't assign an int to a short. a += b; would silently cast back to short.
|
|
| Apr 4, 2015 at 20:43 | comment | added | most venerable sir | Is this what you mean by casting? I am at elementary lvl. I usually use the word "casting" for converting between int and double(truncation). | |
| Apr 4, 2015 at 20:41 | comment | added | most venerable sir | How does it have to do with concatenation? You mean a being a string object, and b is of other type. So b gets forced to become a string object? | |
| Apr 4, 2015 at 20:27 | comment | added | Tom Hawtin - tackline | @user132522 Yes. (Although with other types there may be casting involved.) | |
| Apr 4, 2015 at 20:22 | comment | added | most venerable sir | That expression, a+=b. Doesn't it mean: a=a+b? | |
| Dec 2, 2014 at 20:57 | comment | added | supercat |
I wonder why the Java compiler uses StringBuilder even when joining two strings? If String included static methods to concatenate up to four strings, or all the strings in a String[], code could append up to four strings with two object allocations (the result String and its backing char[], neither one redundant), and any number of strings with three allocations (the String[], the result String, and the backing char[], with only the first being redundant). As it is, using StringBuilder will at best require four allocations, and will require copying every character twice.
|
|
| Sep 15, 2014 at 16:03 | comment | added | Hot Licks | You can consult the JVM spec to understand the individual bytecodes. The stuff you'd want to reference is in chapter 6. A bit obscure, but you can get the gist of it fairly easily. | |
| Sep 15, 2014 at 15:53 | history | rollback | Tom Hawtin - tackline |
Rollback to Revision 3
|
|
| S Sep 15, 2014 at 10:19 | history | suggested | Malwinder Singh | CC BY-SA 3.0 |
full form revised
|
| Sep 15, 2014 at 10:03 | review | Suggested edits | |||
| S Sep 15, 2014 at 10:19 | |||||
| Aug 14, 2014 at 15:08 | history | edited | jameshfisher | CC BY-SA 3.0 |
clarifications, e.g. inline the expression "the former"
|
| Nov 6, 2013 at 21:04 | history | edited | Maarten Bodewes | CC BY-SA 3.0 |
added strictness of receiving string instead of other object or basic type for concat()
|
| Aug 19, 2013 at 20:33 | review | Suggested edits | |||
| Aug 19, 2013 at 20:46 | |||||
| Jun 25, 2013 at 17:58 | comment | added | Tom Hawtin - tackline |
@HyperLink You can see the code using javap -c on a compiled class that uses it. (Oh, as in the answer. You just need to interpret the bytecode disassembly, which shouldn't be that difficult.)
|
|
| Sep 9, 2008 at 3:40 | vote | accept | shsteimer | ||
| Sep 6, 2008 at 16:25 | history | answered | Tom Hawtin - tackline | CC BY-SA 2.5 |