Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why StringBuffer and StringBuilder append methods discard the value returned by the super call

Tags:

java

This is a question out of curiousity. Today I took a look at the implementations of StringBuilder and StringBuffer. Here's the append() method:

public StringBuilder append(String str) {
    super.append(str);
    return this;
}

AbstractStringBuilder.append(str) also returns this. Are there any benefits of discarding the return value (in StringBuilder.append(..)) and returning this again, instead of casting the return value of the super call to the current concrete implementation.

like image 924
Bozho Avatar asked Mar 28 '26 22:03

Bozho


1 Answers

My first thought is that casting can be expensive, so if it can be avoided that would be best.

like image 97
Adam Batkin Avatar answered Mar 31 '26 05:03

Adam Batkin