5

Out of the two methods in StringBuilder's append, which of the following code is better?

stringBuilder.append('\n'); 

or

stringBuilder.append("\n"); 
5
  • 8
    Better in what way? They are equivalent, the first may have slightly less overhead, but that is likely a trivial difference. Commented Apr 25, 2018 at 9:32
  • If you pass a string into append then some bit of code somewhere will have to iterate through the string and add every character in it. But there is unlikely to be a measurable performance difference. Commented Apr 25, 2018 at 9:33
  • 4
    I prefer to use System.lineSeperator (). Commented Apr 25, 2018 at 9:35
  • 2
    In the first case you're appending a char to the StringBuilder. In the second case you're using String to append. This is really trivial question, I'd consider the @GeorgeZougianos comment. Commented Apr 25, 2018 at 9:40
  • You should really use System.lineSeparator(). If \n is the wrong on your system (like on Windows) then the code won't work as expected and that's not worth the picosecond you get with the faster version. Commented Apr 25, 2018 at 10:15

1 Answer 1

12

Appending a single char (stringBuilder.append('\n')) requires less work than appending a String (such as "\n"), even if the String contains only a single character.

Compare append(char c), which basically performs a single assignment to the value array:

public AbstractStringBuilder append(char c) { ensureCapacityInternal(count + 1); value[count++] = c; return this; } 

To append(String str), which requires additional 2 method calls (str.getChars() and System.arraycopy):

public AbstractStringBuilder append(String str) { if (str == null) return appendNull(); int len = str.length(); ensureCapacityInternal(count + len); str.getChars(0, len, value, count); count += len; return this; } 

which calls

public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) { if (srcBegin < 0) { throw new StringIndexOutOfBoundsException(srcBegin); } if (srcEnd > value.length) { throw new StringIndexOutOfBoundsException(srcEnd); } if (srcBegin > srcEnd) { throw new StringIndexOutOfBoundsException(srcEnd - srcBegin); } System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin); } 

Therefore, in terms of performance, stringBuilder.append('\n') is better than stringBuilder.append("\n").

That said, in the specific case of \n, you might want to use a third option - stringBuilder.append(System.lineSeperator ()) . While this has the downside of appending a String (which is slower than appending a char), it accounts for the fact that different platforms (for example Linux vs. Windows) use different line separators, which sometimes even consist of more than one character. Hence stringBuilder.append(System.lineSeperator ()) can be considered more correct.

Sign up to request clarification or add additional context in comments.

6 Comments

why faster than me why? +1
yea today I also want to know why ?
That's definitely faster in terms of performance, but I would love to know this from coding practices as well.
Agree with the performance aspect, disagree with System.lineSeparator() being more correct. It simply depends on the use case.
@xehpuk well, if your Java application only targets a single OS, you are probably safe with using the explicit end-of-line character[s] for that OS.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.