0

Assume that we have a StringBuilder or StringBuffer like below:

StringBuilder s1 = new StringBuilder("xxx");
StringBuffer s2 = new StringBuffer("xxx");

We can get a String from the above variables using the toString() method.

I know that toString is an over-ridden method in both classes.

Assume also that we have a String like below:

String s3 = "xxx";

I can't get a StringBuilder or StringBuffer using toStringBuilder() or toStringBuffer(). Instead, the way we can achieve is like below:

StringBuilder sb = new StringBuilder(s3);

Is there a reason why toStringBuilder()/toStringBuffer() is not defined and is there any other effective way to get a StringBuilder/StringBuffer from String.

4
  • 5
    What benefit would there be, in your view? What do you dislike about calling new StringBuilder(s)? Commented Jun 6, 2014 at 12:17
  • A mutable StringBuilder wraps an immutable String, so why do you want to get a wrapping instance where a String doesn't even have to know, that something like a StringBuilder exists? Commented Jun 6, 2014 at 12:17
  • If you're using Scala you can create an implicit class over the java.lang.String class and create your own .toStringBuilder() method. Commented Jun 6, 2014 at 12:18
  • @JonSkeet : i am just checking the reason why they have not given the same method what they have given in case of stringbuilder class Commented Jun 6, 2014 at 12:36

3 Answers 3

2

String itself is immutable. That has many advantages, especially with concurrent usage, sharing substrings and such.

Also back and forth coupling between classes is bad design.

Sign up to request clarification or add additional context in comments.

5 Comments

"String itself is immutable". Yes it is. But what's that got to do with the question? .toStringBuilder would not modify the String but make a new StringBuilder with the same content.
i have same question like Paul ...and coupling is already done in case of stringbuilder.toString();
@KaribasappaGC: No, there isn't two-way coupling. StringBuilder "knows about" String, but the String API doesn't know about StringBuilder at all.
@Paul yes, there my argumentation is unclear. I meant more or less that a StringBuilder won't back its data to the String. Or so.
Well,it might. As long as it doesn't modify it, there's no particular reason why StringBuilder shouldn't refer to the original string as its initial contents. It would make (new StringBuilder("Foo")).toString quite efficient :)
0

toString is a method that is part of the Object class and thus all classes of java will have such method. A String is immutable and as such both toStringBuilder and toStringBuffer will create new objects. I think that if there were such methods many users would be mislead that using these methods they can modify the string in-place. Also because of the same reasons there can not possible be any more efficient way to create a StringBuilder/StringBuffer.

1 Comment

"many users would be mislead that using these methods they can modify the string in-place" Really? Do people really think .toString modifies the actual content of the StringBuilder, rather than making a new String out of it?
0

You might ask for a considerable number of String.toFoo() methods, and where to draw the line? You can construct StringBuilder and StringBuffer using the constructor. That's enough - why add another method?

2 Comments

we usually do lot of conversions between string and string buffer and string builder..atleast in my experience...so this question comes..its not on any user defined class like Foo
You have the StringBu*er constructors - why should String be burdened with more methods? What would it achieve? Why would code be better that way?

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.