Copy a String in Java

Rashmi Patidar Jun 17, 2021
Copy a String in Java

In the Java language, a String is a data type that stores a sequence of characters. A string is a wrapper class that provides methods like compare(), replace(), and substring(). The objects are stored in the heap memory whenever an object gets instantiated.

Copy a String in Java

Below is the code block that shows you how to copy a string in Java.

public class StringCopy {
    public static void main(String[] args) {
        String first = "First String";
        System.out.println("First initially = " + first);
        String second = first;
        System.out.println("String copy in second = " + second);
        first = "Updated string";
        System.out.println("First after update = " + first);

        String newCopy = String.copyValueOf(first.toCharArray());
        System.out.println("Copy using copyValueOf() = " + newCopy);

        String copyString = new String(first);
        System.out.println("Copy using new = " + copyString);

In the program above, a string gets initialized on the first part of the operation. The representation String first = "First String" creates an instance of First String in the memory; additionally, this new string address gets assigned to the variable first. This value gets printed using the println() method.

Now, the String second = first line stores the first reference in the second instance and prints the copy value in another line. As a result, the second variable contains the first reference. Then, the first = "Updated string" will change the reference of the first one from the existing value to another string in the heap memory.

Java Strings are immutable in nature. It means that when a string gets created, it is there in heap memory. And when another value gets assigned to the first variable, it is not overridden. Instead, the variable now refers to another string from the heap memory. The references in the heap memory are stored into the heap unless the garbage collector comes into action. The function flushes out the variable from the constant memory pool when not referenced by any variable.

Another way to copy a string uses the copyValueOf method. It is a static factory method that takes a character array as an input. The instance is first converted to a character array using the toCharArray function. The final string instance gets referenced by a newCopy variable and printed in another line.

The third way to copy a string is by using the new keyword. The method creates two instances in the memory: the first holds the value, and the other copyString variable stores the reference of the first variable.

Below is the code block that results from the program above.


First initially = First String
String copy in second = First String
First after update = Updated string
Copy using copyValueOf() = Updated string
Copy using new = Updated string
Rashmi Patidar avatar Rashmi Patidar avatar

Rashmi is a professional Software Developer with hands on over varied tech stack. She has been working on Java, Springboot, Microservices, Typescript, MySQL, Graphql and more. She loves to spread knowledge via her writings. She is keen taking up new things and adopt in her career.


Related Article - Java String