Java String copyValueOf() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In Java, String.copyValueOf() builds a new string from a char[]. It is a static method, so you call it on String, not on an instance.

You pass the array, or the array plus offset and count. The array is required. Later edits to the array do not change the string.

Syntax

Call copyValueOf on the String class. Start with this line.

java
String s = String.copyValueOf(data);
CallWhat it does
String.copyValueOf(data)Builds a string from the whole char array.
String.copyValueOf(data, 1, 2)Builds a string from 2 characters starting at offset 1.

This is the same idea as valueOf(char[]). Call it on String, not on a string instance.

Library declarations
java
public static String copyValueOf(char[] data);
public static String copyValueOf(char[] data, int offset, int count);

Parameters

ParameterDescription
dataRequired. The char array to copy from.
offsetOptional. Where to start in the array.
countOptional. How many characters to take.

Return Value

Return ValueDescription
New StringA string made from the chosen characters.

Java Examples

Example 1 — Copy the Full Array

Turn every character into one String.

java
public class CopyValueOfBasic {
    public static void main(String[] args) {
        char[] data = {'J', 'a', 'v', 'a'};
        String s = String.copyValueOf(data);

        System.out.println(s);
    }
}

How It Works

  1. Build a char array. Four letters spell Java.
  2. Call the static method. String.copyValueOf(data) copies all entries.
  3. Get a new String. Result is "Java".
  4. Print it. Confirms the conversion worked.

Example 2 — Copy a Range

Take only part of a buffer with offset and count.

java
public class CopyValueOfRange {
    public static void main(String[] args) {
        char[] data = {'C', 'o', 'd', 'e'};
        String mid = String.copyValueOf(data, 1, 2);
        String last = String.copyValueOf(data, 2, 2);

        System.out.println(mid);
        System.out.println(last);
    }
}

How It Works

  1. Start at index 1. count 2 copies 'o' and 'd' → "od".
  2. Start at index 2. Next two chars are 'd' and 'e' → "de".
  3. Handy for buffers. You can ignore unused prefix/suffix slots.
  4. Stay in bounds. offset + count must not pass the array length.

Example 3 — Same Result as new String(char[])

Full-array copyValueOf matches the String constructor; pick the style you prefer.

java
public class CopyValueOfVsCtor {
    public static void main(String[] args) {
        char[] data = {'H', 'i'};

        String a = String.copyValueOf(data);
        String b = new String(data);

        System.out.println(a);
        System.out.println(b);
        System.out.println(a.equals(b));
    }
}

How It Works

  1. Same source. Both paths read {'H','i'}.
  2. Two construction styles. Static helper vs constructor.
  3. Equal content. equals returns true.
  4. Prefer clarity. Use copyValueOf when the “copy from buffer” intent matters most.

Frequently Asked Questions

copyValueOf() builds a new String from a char array.
Yes. Call String.copyValueOf(...).
data is required. offset and count are used in the range overload.
A new String made from the characters.
No. The string keeps its own copy of the characters.

Next: describeConstable()

Get an Optional holding the string as its own constant descriptor (Java 12+).

describeConstable() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful