Tutorial

Java String Copy

Published on August 3, 2022
author

Pankaj

Java String Copy

Sometime back I was asked how to copy a String in java. As we know that String is an immutable object, so we can just assign one string to another for copying it. If the original string value will change, it will not change the value of new String because of immutability.

Java String Copy

Here is a short java String copy program to show this behavior.

package com.journaldev.string;

public class JavaStringCopy {

	public static void main(String args[]) {
		String str = "abc";

		String strCopy = str;

		str = "def";
		System.out.println(strCopy); // prints "abc"

	}
}

Note that we can perform direct assignment of one variable to another for any immutable object. It’s not limited to just String objects. However, if you want to copy a mutable object to another variable, you should perform deep copy.

Java String Copy Alternate Methods

There are few functions too that can be used to copy string. However it’s not practical to use them when you can safely copy string using assignment operator.

  1. Using String.valueOf() method

    String strCopy = String.valueOf(str);
    
    String strCopy1 = String.valueOf(str.toCharArray(), 0, str.length()); //overkill*2
    
  2. Using String.copyValueOf() method, a total overkill but you can do it.

    String strCopy = String.copyValueOf(str.toCharArray());
    
    String strCopy1 = String.copyValueOf(str.toCharArray(), 0, str.length()); //overkill*2 
    

If you want to copy part of string to another string, then valueOf and copyValueOf methods are useful.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar
Pankaj

author

While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
January 3, 2020

At least in Java 11 (I believe this has been true for a lot longer than that), `String.valueOf(str)` will return the same instance of `str`, so it’s functionally identical to directly assigning the reference. In String.java: public String toString() { return this; } public static String valueOf(Object obj) { return (obj == null ? “null” : obj.toString(); } When obj is a String, String.toString() is called, and this returns the string itself and not a copy. If you want a full copy of the original string, you’ll need to use the String constructor or any of the other methods you listed.

- Kiefer

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Featured on Community

    Get our biweekly newsletter

    Sign up for Infrastructure as a Newsletter.

    Hollie's Hub for Good

    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

    Become a contributor

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    Welcome to the developer cloud

    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

    Learn more