Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java String copyValueOf() method
Syntax and Examples



Introduction

The copyValueOf() method in Java's String class provides a convenient way to create a new string from a character array. It essentially copies a portion of the character array into a newly created String object. There are two overloaded versions:

  • One takes the entire character array as input.
  • The other allows you to specify an offset and count, letting you copy only a specific segment of the array.

Syntax


public static String copyValueOf(char[] data)
public static String copyValueOf(char[] data, int offset, int count)

Parameters Table

Parameter Description
data The character array from which the String will be created.
offset The index within data where copying begins.
count The number of characters to copy from data, starting at the specified offset.

Return Value

A new String object containing a copy of the specified portion of the character array.

Examples

Example 1: Copying the Entire Array

This example demonstrates how to create a String from a character array using the first version of copyValueOf(), which copies all characters in the array.


char[] charArray = {'J', 'a', 'v', 'a'}; 
String str = String.copyValueOf(charArray);
System.out.println(str);
Java

Explanation: The charArray holds the characters 'J', 'a', 'v', and 'a'. The copyValueOf() method takes this array and creates a new String object containing those same characters, which is then printed to the console.

Example 2: Copying a Portion of the Array

This example shows how to use the second version of copyValueOf(). It extracts a portion of a character array starting at a given offset and copying a specified number of characters, creating a new String.


char[] charArray = {'H', 'e', 'l', 'l', 'o'}; 
String str = String.copyValueOf(charArray, 1, 3);
System.out.println(str);
ell

Explanation: Here, we're using an offset of 1 and a count of 3. This means the copying starts from the second character ('e') in the array and copies the next three characters ('l', 'l', 'o'). The resulting String is then printed to the console.

Example 3: Using with an empty array

This example shows how the method behaves when passed an empty char array.


char[] charArray = {}; 
String str = String.copyValueOf(charArray);
System.out.println("|" + str + "|");
|

Explanation: When an empty character array is passed to copyValueOf(), the method returns an empty String. The `println` statement outputs a pipe symbol on either side of the string to make it clear that an empty string has been created.

Example 4: Using with offset and count beyond boundaries

This example shows what happens when you give an out-of-bounds offset or count.


char[] charArray = {'A', 'B', 'C'}; 
String str1 = String.copyValueOf(charArray, 1, 5); // count exceeds array length
String str2 = String.copyValueOf(charArray, -1, 2); // Negative offset
System.out.println("|" + str1 + "|");
System.out.println("|" + str2 + "|");
|ABC|

Explanation: When the count exceeds the boundaries of the array, or when a negative offset is provided, Java's copyValueOf() method gracefully handles this. It copies up to the maximum possible number of characters that can be extracted given the array bounds.



Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M