how the append method of StringBuilder in Java works, and why the string passed to it as a parameter doesn't create a new string object in memory - Stack Overflow
Implementing append method of stringbuilder class
Java StringBuilder append methods chain - Stack Overflow
java - Appending a StringBuilder to another StringBuilder - Stack Overflow
Videos
sb.append(" Good") returns a reference to the same StringBuilder instance on which the method was called, which allows you to chain another .append() call to it.
StringBuilder sb = new StringBuilder();
sb.append("Teacher,");
String s = sb.append(" Good").append("Morning!").toString();
is equivalent to
StringBuilder sb = new StringBuilder();
sb.append("Teacher,");
sb.append(" Good");
sb.append("Morning!");
String s = sb.toString();
append() on StringBuilder just returns this as a convenience.
String s = new StringBuilder().append("Good").append(" Morning!").toString();
is equivalent to
StringBuilder sb = new StringBuilder();
sb.append("Good");
sb.append(" Morning!");
String s = sb.toString();
StringBuilder has a public StringBuilder append(CharSequence s) method. StringBuilder implements the CharSequence interface, so you can pass a StringBuilder to that method.
There are plenty of append() overloads, including append(CharSequence cs). Strings are handled in their own method, other CharSequences not having their own overload are handled by the more general one.
Interestingly there is an append(StringBuffer sb).
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");
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.
So I get that StringBuilders are more efficient than concatenating strings with the "+" operator, because they are manipulating the underlying char[] array. But here's what I don't understand.
Let's look at case 1, where we are appending a string to the StringBuilder inside the parentheses. Doesn't this mean that for every time we call this statement, a new string has to be created and then appended to the StringBuilder?
StringBuilder().append("my new string"); // "my new string" needs to be created every time!
Now let's look at case 2, where we create a premade string, and then we append it to the StringBuilder. Wouldn't this be more efficient, because we can loop through the append statement as many times as we want without creating a new String?
String myString = "hello world";
StringBuilder.append(myString); // myString is already created!
Hope this makes sense. Asking because I'm writing a program that will potentially need to do this same operation millions of times per day, and want to get a better understanding.