Java
Strings
Copy methods
Programming
Software development

How to copy a String in Java

Codemia Knowledge Hub ·

Because Java strings are immutable, you usually do not need to copy one at all: String copy = original; gives you a second reference to the same object, and nothing can change that object through either variable. If you genuinely need a distinct object, use new String(original).

Immutability and the string pool

  • Immutability: Once a String is created, it cannot be changed. Any "modification" produces a new string.
  • String pool: Java keeps a pool of string literals (and interned strings) to save memory. Literals with the same content share one pooled instance.

Ways to copy a string

1. Assignment

The simplest option is the assignment operator (=). It does not create a new string, only a new reference to the same instance.

java
String original = "Hello";
String copy = original;

Both variables point to the same object. Assigning new text to either one does not affect the other, because the assignment just repoints that variable; the string itself never changes.

2. new String(String original)

To force a separate object, use the constructor:

java
String original = "Hello";
String copy = new String(original);

This creates a new String instance with the same value. The new object lives on the heap outside the string pool; only the literal "Hello" is pooled. copy == original is false, while copy.equals(original) is true.

3. String.valueOf()

java
String original = "Hello";
String copy = String.valueOf(original);

For a String argument, String.valueOf() returns the same string (it calls toString(), which returns this), so this does not create a copy.

4. String.concat("")

java
String original = "Hello";
String copy = original.concat("");

concat returns the original string object when the argument is empty, so this does not produce a new object either. It is only an indirect way of writing an assignment.

5. Encoding and decoding bytes

java
String original = "Hello";
String copy = new String(original.getBytes());

This converts the string to bytes with the platform's default charset and builds a new string from them. It does create a new object, but it is very inefficient for plain duplication.

Comparing the methods

MethodExplanationMemory UseEfficiency
Assignment OperatorAdds a reference to the existing string.Shared memoryHighly efficient
new String(String)Creates a new string object in memory.Additional spaceLess efficient
String.valueOf()Returns the same string object for a String argument.Shared memoryEfficient
String.concat("")Returns the same string object when the argument is empty.Shared memoryNeedless call
Encoding/Decoding BytesConverts string to bytes and back, creating a new object.Additional spaceInefficient

String copying can affect performance in large applications or tight loops. Share references with assignment, and only create a new object when a distinct instance is really required.

Practical advice

  • Avoid new unless you need it: Copying a reference never requires new. Use it only when a separate instance is essential.
  • Let pooling work for you: In applications with many repeated strings, rely on Java's string pooling through interning.
  • Use StringBuilder or StringBuffer for mutable text: When contents change extensively, use StringBuilder (not thread-safe) or StringBuffer (thread-safe) to keep overhead down.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms