Java - String copyValueOf() Method



The Java String copyValueOf() method returns a String that represents the character sequence in the array specified. It creates a new array and inserts the characters into it.

The copyValueOf() method is a static method, which means it can be accessed directly using the class name itself.

This method has two polymorphic variants with different parameters: int, char [].(Below are the syntaxes of all the polymorphic variants).

Syntax

Following is the syntax of the Java String copyValueOf() method −

public static String copyValueOf(char[] data)// first syntax
public static String copyValueOf(char[] data, int offset, int count)// second syntax

Parameters

  • data − This is the character array.

  • offset − Initial offset of the subarray.

  • count − Length of the subarray.

Return Value

This method returns a String that contains the characters of the character array.

Example

If the character of an array is not null, the copyValueOf() method returns a String that represents the characters of the char array.

In the following program, we are creating a char array with the value ‘j’, ‘a’, ‘v’, and ‘a’. Using the copyValueOf() method, we are trying to retrieve the string that represents the characters of the char array.

import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      
      // create an character array
      char ch[] = {'j','a','v','a'};
      System.out.print("The char array elements are: ");
      for(int i = 0; i<ch.length; i++) {
         System.out.print(ch[i] +  " ");
      }
      
      // using copyValueOf() method
      String str = String.copyValueOf(ch);
      
      // prints the string
      System.out.println("\nThe string represents the characters of the char array is: " + str);
   }
}

Output

On executing the above program, it will produce the following result −

The char array elements are: j a v a 
The string represents the characters of the char array is: java

Example

If the given character array is null, this method throws NullPointerException.

In the following example, we are creating a char array with the null value. Using the copyValueOf() method, we are trying to retrieve the string that represents the characters of the given char array. Since the char array is null, it throws an exception.

import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      try {
         
         // create an character array
         char ch[] = null;
         System.out.print("The char array elements are: ");
         for(int i = 0; i<ch.length; i++) {
            System.out.print(ch[i] +  " ");
         }
         
         // using copyValueOf() method
         String str = String.copyValueOf(ch);
         
         // prints the string
         System.out.println("The string represents the charcaters of the char array is: " + str);
      } catch(NullPointerException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

Following is the output of the above program −

The char array elements are: java.lang.NullPointerException: Cannot read the array length because "ch" is null
	at com.tutorialspoint.String.Demo.main(Demo.java:9)
Exception: java.lang.NullPointerException: Cannot read the array length because "ch" is null

Example

If the given char array is not null, and the offset and count argument value has passed to the method, the copyValueOf() method returns the string that represents the characters of the char array.

In this program, we are creating a char array with the value ‘H’, ‘E’, ‘L’, ‘L’, and ’O’. Using the copyValueOf() method, we are trying to retrieve the string that represents the characters of the char array, where the offset value is 1, and the count value is 4.

import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      try {
         
         // create an character array
         char ch[] = { 'H', 'E', 'L', 'L', 'O'};
         System.out.print("The char array elements are: ");
         for(int i = 0; i<ch.length; i++) {
            System.out.print(ch[i] +  " ");
         }
         
         //initialize the offset and count value
         int offset = 1, count = 4;
         System.out.println("\nThe offset and count values are: " + offset + " and " + count);
         // using copyValueOf() method
         String str = String.copyValueOf(ch, offset, count);
         
         // prints the string
         System.out.println("The string represents the characters of the char array is: " + str);
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

The above program, produces the following output −

The char array elements are: H E L L O 
The offset and count values are: 1 and 4
The string represents the characters of the char array is: ELLO	

Example

If the given offset value is negative, the copyValueOf() method throws the StringIndexOutOfBoundsException.

In the following program, we are creating a char array with the value ‘H’, ‘E’, ‘L’, ‘L’, and ’O’. Using the copyValueOf() method, we are trying to retrieve the string that represents the characters of the char array, where the offset value is -1, and the count value is 2. Since the offset value is negative, it throws an exception.

package com.tutorialspoint.String;
import java.lang.*;
public class Demo {
   public static void main(String[] args) {
      try {
         
         // create an character array
         char ch[] = { 'H', 'E', 'L', 'L', 'O'};
         System.out.print("The char array elements are: ");
         for(int i = 0; i<ch.length; i++) {
            System.out.print(ch[i] +  " ");
         }
         
         //initialize the offset and count value
         int offset = -1, count = 2;
         System.out.println("\nThe offset and count values are: " + offset + " and " + count);
         
         // using copyValueOf() method
         String str = String.copyValueOf(ch, offset, count);
         
         // prints the string
         System.out.println("The specified returned string is: " + str);
      } catch(IndexOutOfBoundsException e) {
         e.printStackTrace();
         System.out.println("Exception: " + e);
      }
   }
}

Output

After executing the above program, it generates the following output −

The char array elements are: H E L L O 
The offset and count values are: -1 and 2
java.lang.StringIndexOutOfBoundsException: offset -1, count 2, length 5
	at java.base/java.lang.String.checkBoundsOffCount(String.java:4589)
	at java.base/java.lang.String.rangeCheck(String.java:304)
	at java.base/java.lang.String.(String.java:300)
	at java.base/java.lang.String.copyValueOf(String.java:4273)
	at com.tutorialspoint.String.Demo.main(Demo.java:17)
Exception: java.lang.StringIndexOutOfBoundsException: offset -1, count 2, length 5
java_lang_string.htm
Advertisements