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
Stringis 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.
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:
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()
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("")
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
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
| Method | Explanation | Memory Use | Efficiency |
| Assignment Operator | Adds a reference to the existing string. | Shared memory | Highly efficient |
new String(String) | Creates a new string object in memory. | Additional space | Less efficient |
String.valueOf() | Returns the same string object for a String argument. | Shared memory | Efficient |
String.concat("") | Returns the same string object when the argument is empty. | Shared memory | Needless call |
| Encoding/Decoding Bytes | Converts string to bytes and back, creating a new object. | Additional space | Inefficient |
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
newunless you need it: Copying a reference never requiresnew. 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
StringBuilderorStringBufferfor mutable text: When contents change extensively, useStringBuilder(not thread-safe) orStringBuffer(thread-safe) to keep overhead down.
Related reading
- How should I map long to int in hashCode?
- How should I use JpaRepository.findOne with SpringBoot?
- How should I use servlets and Ajax?
- How Spring Boot Application works internally?
- How Spring Boot run batch jobs
- How Spring Kafka Consumer skips from Avro Deserializer exception
- How to access a Spring Boot Application's name programmatically?
- How to access a value defined in the application.properties file in Spring Boot
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.