2

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?

2

5 Answers 5

5

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");
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer. But how should I know whether this method has this "builder pattern" or not?
As I said earlier, most of the methods from that class return the same type and methods have return this
Here is an example implementation en.wikipedia.org/wiki/Builder_pattern#Java
Ok, I got it. Thanks for helping me.
3

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.

Comments

2

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"

Comments

2

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.