There's nothing different between line 6 and line 8.
PrintStream.println(Object) calls string.valueOf(Object) which calls the object's toString() method, and that gets printed.
System.out.println(s) and System.out.println(s.toString()) have the same output (unless s is null, in which case the latter throws an exception).
The reason you call s.toString() directly is to get the current "built" value from the StringBuilder as a string so you can pass it on to other code which expects a string. If the code you want to call takes a StringBuilder you could just pass s, but then the called code has the ability to modify your value (they can't with string because it is an immutable data type).
java - Why do we need to use toString method with StringBuilder? - Stack Overflow
java - Using StringBuilder instead of toString() - Stack Overflow
What happens when i use toString() method on a StringBuilder?
java - Does StringBuilder need .ToString()? - Stack Overflow
There's nothing different between line 6 and line 8.
PrintStream.println(Object) calls string.valueOf(Object) which calls the object's toString() method, and that gets printed.
System.out.println(s) and System.out.println(s.toString()) have the same output (unless s is null, in which case the latter throws an exception).
The reason you call s.toString() directly is to get the current "built" value from the StringBuilder as a string so you can pass it on to other code which expects a string. If the code you want to call takes a StringBuilder you could just pass s, but then the called code has the ability to modify your value (they can't with string because it is an immutable data type).
System.out.println can print out arrays of chars (line6) s.toString() is used to actually place the stringbuilder contents into a string. in your code you are not setting it to a string variable so nothing will happen... so on line8 the array of chars is printed again from the contents of your string builder.
if you add string str = s.toString(); System.out.println(str);
then the string will print the string variable made from the stringbuilder.
If this is for a toString() override or even if it's not, then it must return a String, not a StringBuilder. Else it's not a true override, and it shouldn't even compile since the method signature is the same as Object's toString() but with a different return type.
Instead, sure use a StringBuilder internally within the method body, but return the StringBuilder's toString().
e.g.,
@Override // this must return String
public String toString() {
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("Data in Kennel " + name + " is:");
for (Animal d : dogs) {
strBuilder.append(d.toString() + "\n");
}
return strBuilder.toString();
}
It seems you are trying to make a method like Object's toString() which returns a StringBuilder. Don't do this. Use StringBuilder and you can convert it to a String using toString().
StringBuilder strb = new StringBuilder("SampleString");
String str = strb.toString();
So let's say i have a code like this:-
StringBuilder str = new StringBuilder("hello");
System.out.println(str.toString()); So is it like there are two arrays, StringBuilder and String and we loop StringBuilder and copy it's contents character by character into the String? What would be the time complexity of this process?
In Java, you can't define implicit conversions between types, beyond what's in the specification - so you can't convert from StringBuilder to String anyway; you have to call toString().
In C# there could be an implicit user-defined conversion between StringBuilder and String (defined in either StringBuilder or String), but there isn't - so you still have to call ToString().
In both cases you will get a compile-time error if you don't call the relevant method.
In C#, you need to use string foobar = sb.ToString(); or you will get an error:
Cannot implicitly convert type 'System.Text.StringBuilder' to 'string'.
You can use .ToString() to get the String from the StringBuilder.
When you say "it's faster to concatenate strings with a StringBuilder", this is only true if you are repeatedly (I repeat - repeatedly) concatenating to the same object.
If you're just concatenating 2 strings and doing something with the result immediately as a string, there's no point to using StringBuilder.
I just stumbled on Jon Skeet's nice write up of this:
https://jonskeet.uk/csharp/stringbuilder.html
If you are using StringBuilder, then to get the resulting string, it's just a matter of calling ToString() (unsurprisingly).
The toString implementation currently just redirects to the result method anyway, so those two methods will behave in the same way. However, they express slightly different intent:
toStringrequests a textual representation ofStringBuilders current state that is "concise but informative (and) that is easy for a person to read". So, theoretically, the (vague) specification of this method does not forbid abbreviating the result, or enhancing conciseness and readability in any other way.resultrequests the actual constructed string. No different readings seem possible here.
Therefore, if you want to obtain the resulting string, use result to express your intent as clearly as possible.
In this way, the reader of your code won't have to wonder whether StringBuilder.toString might shorten something for the sake of "conciseness" when the string gets over 9000 kB long, or something like that.
The mkString is for something else entirely, it's mostly used for interspersing separators, as in "hello".mkString(",") == "h,e,l,l,o".
Some further links:
- The paragraph with "hashcode in hexadecimal" describes the default. It is just documentation inherited from
AnyRef, because the creator ofStringBuilderdidn't bother to provide more detailed documentation. - If you look into code, you'll see that
toStringis actually just delegating toresult. - The documentation of
StringBuilderalso mentionsresult()in the introductory overview paragraph.
Just use result().
TL;DR; use result as stated in the docs.
toString MUST never be called in anything at all for another purpose other than a quick debug.
mkString is inherited from collections hierarchy and it will basically create another StringBuilder so is very inefficient.