StringBuilder str = new StringBuilder("Today");
str.append(" is ").append("a").append(" sunny ").append(" day.");
In the java code above, I understand that first I created an object of type StringBuilder. Then I used the object reference str to access the method append of the class StringBuilder. After this, I loose track. Is the method append used after str.append(“is”) inside the append method, or am I calling the same method in this class? Further, can anyone explain the flow of execution of the above statement. Which of the above append methods is executed first?
str.append(" is ")returns the StringBuilder itself. You’re calling a method on the object returned by the method. It’s the same as doingExcept in your code, each
append()method call returns the same object, which allows chaining multiple method calls to the same StringBuilder.