kevinHuang
kevinHuang

Reputation: 165

Java StringBuilder append() return?

I'm just new in Java programming and it's really confusing me that what's the return type of method append() in StringBuilder??

I've checked about the API documents on https://docs.oracle.com/javase/9/docs/api/index.html?java/lang/String.html and the return is "StringBuilder", so I should write the code like:

StringBuilder a=new StringBuilder("hello");
...(another StringBuilder object)=a.append("world");

and a will still be "hello", and another stringbuilder will become"helloworld" because it has a return value??

But actually a itself also become "helloworld". Why??Did I misunderstand something?

Upvotes: 2

Views: 4442

Answers (5)

Naresh_Nalla
Naresh_Nalla

Reputation: 21

StringBuilder actually returns this ( current object) in append method of it.

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 122026

It is reasonable that StringBuilder follows the Builder Pattern, as it is used for building a String.

In a builder pattern each method (mostly) returns the current instance so that the return is a modified instance.

But actually a itself also become "helloworld". Why??

Because you are using the same instance to append again., so it already stored the previous value and was given the updated instance.

so I should write the code like:

StringBuilder a=new StringBuilder("hello"); ...(another StringBuilder object)=a.append("world");

You need not to. Since that returning the instance you can chain your method calls. That is the beauty of builder pattern.

StringBuilder a=new StringBuilder("hello");
a.append("World").append(" Mr.").append("Blah");

Upvotes: 5

Christophe Roussy
Christophe Roussy

Reputation: 17049

This is in order to allow chaining:

sb.append("a").append("b").append("c"). ...

Without this you would have to repeat sb: sb.append("a"); sb.append("b"); ...

This pattern is common in builders and most famous with jQuery. In general this can be useful, but when the chain becomes hard to read it can also introduce bugs.

Upvotes: 2

jrtapsell
jrtapsell

Reputation: 7031

Why append returns a StringBuilder

So that you can append "a", "b" and "c" like this:

StringBuilder sb = new StringBuilder()
        .append("a")
        .append("b")
        .append("c");

What is returned

The StringBuilder returned is the same as the one that was called on, so this:

StringBuilder sb1 = new StringBuilder();
StringBuilder sb2 = sb1.append("a")
        .append("b")
        .append("c");
System.out.println(sb1 == sb2);

Prints out true, this is why the 2 StringBuilders in your example had the same text, because they are the same StringBuilder.

Upvotes: 3

Mena
Mena

Reputation: 48444

The StringBuilder class (as well as its thread-safe sibling, StringBuffer) offers a fluent builder pattern.

Most methods return the modified instance of your StringBuilder after applying the method, so you can chain-invoke methods on it.

You an either invoke append as you would a void returning method, and disregard the returned StringBuilder for one shot operations, or you can chain invocations, in the safety that you're still manipulating the same StringBuilder step by step.

Example

StringBuilder sb = new StringBuilder();
sb.append("a"); // void-like invocation
sb.append("b").append("c"); // chained invocation
// terminal invocation as it doesn't return an instance of 
// StringBuilder, but (immutable) String
String s = sb.toString(); // "abc"

Upvotes: 2

Related Questions