0

I would like to make a copy of a string without having a string variable reference the previous string. Would the toString() method be the solution?

In other words, does String.toString() return a copy of its characters rather than a reference to itself?

Josiah Savoie
  • 135
  • 12
  • 7
    *Why* do you want to make a copy of a string? As they're immutable? (E.g., can't be changed.) So there's no reason not to share them between things using them... – T.J. Crowder Dec 27 '13 at 17:53
  • 1
    Questions like that are easily answered by googling it yourself: google java api String or toString : http://docs.oracle.com/javase/7/docs/api/java/lang/String.html http://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString() – K.C. Dec 27 '13 at 17:55
  • [Strings are immutable](http://ideone.com/jAoR6Z). Don't waste your time trying to copy them. – FrankieTheKneeMan Dec 27 '13 at 17:55
  • I know 2 possible reasons: when used as synchronization monitor and result of a substring in Java <7. Both indicate problems upstream. – Guillaume Dec 27 '13 at 17:55
  • possible duplicate of [How to use the toString method in Java?](http://stackoverflow.com/questions/3615721/how-to-use-the-tostring-method-in-java) – Sitansu Dec 27 '13 at 17:57
  • Maybe this will help a little [`String#toString()`](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/lang/String.java?av=f#2789), [`new String(String original)`](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/7-b147/java/lang/String.java?av=f#164). – Pshemo Dec 27 '13 at 18:04
  • 2
    Closed as unclear? and yet look at all the people who clearly understand that the OP is asking about the mutability of strings... – C. Tewalt Jan 05 '16 at 23:10

4 Answers4

7

No, it returns the String object itself. You can do

String copy = new String(myString.toCharArray());

or

String copy = new String(myString); // may use same char[] instance

Please note String is immutable so usually you have no need to copy it.

Guillaume
  • 5,535
  • 1
  • 24
  • 30
  • Why not just `new String(myString)`? – Pshemo Dec 27 '13 at 18:06
  • From the code it re-uses the char array (if length is correct in Java6, always in Java7). – Guillaume Dec 27 '13 at 18:08
  • 1
    I get your point. Anyway it is hard to tell if OP is willing to reuse already existing array of characters in new String or not so this discussion is pointless. It is kind of XY problem so OP will have to tell us more about what he really want to achieve. – Pshemo Dec 27 '13 at 18:12
  • Wow, I'm just looking back at this now after a little over 2 years. Damn this was a stupid question for me to ask. I think I was still learning to get a handle on programming at that point. – Josiah Savoie Mar 09 '16 at 17:02
3

No, toString will return the String itself. If you want a copy you should use

String newString = new String(oldString);

but the internal implementation of strings in JDK uses a string pool so they could refer to the same internal string.

In any case asking for a copy doesn't make sense, since they're immutable: unless you are modifying the copy while you create it, there is no point in doing it at all.

Jack
  • 131,802
  • 30
  • 241
  • 343
0
 String s1 = "123";
 String s2 = "123";

These two are the same string.

T McKeown
  • 12,971
  • 1
  • 25
  • 32
0

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance.

toString(): This returns a String object representing the value of this Integer.

toString(int i): This returns a String object representing the specified integer.

System.out.println("str.toString: " + str.toString());
Sitansu
  • 3,225
  • 8
  • 34
  • 61