I have a String object in my code like
String tempString = "Some String";
now if I write something as
tempString.toString();
will this create another String object in String pool?
No, because toString() method in class String looks like this one:
public String toString() {
return this;
}
As answered by Andremoniy from the code. Here is the part from the javadoc
toString
public String toString()
This object (which is already a string!) is itself returned. Specified by: toString in interface CharSequence Overrides: toString in class Object Returns: the string itself.
So no new object is created in this case. Regarding the use, it's just extra piece of code you are adding and nothing else.
Other interesting read in this respect
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With