Java copyValueOf Method

The Java copyValueOf Method is one of the String Methods, which is to return the string that represents the character sequence in the specified array.

In this article, we will show how to use String copyValueOf with an example. The basic syntax of the string.copyValueOf in Java Programming language is as shown below.

Java copyValueOf Method syntax

The Java Programming Language provides two different String copyValueOf methods to return the string representation. The following method will accept the character array as the parameter and return a string that represents the characters in an array.

public static String copyValueOf(char[] anCharArray); 

//In order to use in program
String.copyValueOf(char[] anCharArray);

The following Java copyValueOf method will accept the character array as the first parameter, Starting index position (Starting_index) as the second argument, and the array count as the third argument. This Java method will start copying values from the Starting_index and return up to user specified count. For example, char[] ch = {‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’} and String.copyValueOf(ch, 2, 4) will return the string as code

public static String copyValueOf(char[] anCharArray, int Starting_index, int count);  

//In order to use in program
String.copyValueOf(char[] anCharArray, int Starting_Index, int count);
  • Starting_Index: Please specify the starting index position. If we specify this value, then instead of copying from the index position 0 copyValueOf String function will start copying from this position.
  • Count: Please specify the length of a sub-array.

Java copyValueOf Method Example

The Java string copyValueOf method returns a string that represents the character sequence.

package StringFunctions;

public class CpyValueOfMethod {
	public static void main(String[] args) {
		char[] ch = {'a', 'b', 'c', 'd', 'e', 'f'};
		System.out.println(String.copyValueOf(ch));
		
		String str = "Hello " + String.copyValueOf(ch);
		System.out.println(str);
		
		String str1 ="Hi";
		str1 = str1 + " Hello " + String.copyValueOf(ch);
		System.out.println(str1);		
	}
}
abcdef
Hello abcdef
Hi Hello abcdef

First, we declared an array of characters using the following Java statement.

char[] ch = {'a', 'b', 'c', 'd', 'e', 'f'};

It will call the public static String copyValueOf (char[] anCharArray) method to return the string representation of the array ch.

System.out.println(String.copyValueOf(ch));

Next, we add some string data to the function return value. It means Hello + abcdef

String str = "Hello " + String.copyValueOf(ch);

copyValueOf Example 2

The Java copyValueOf method returns a string that represents the character sequence in the specified sub-array. This program will help to understand the method.

// Example
package StringFunctions;

public class CopyValueOfMethod2 {
	public static void main(String[] args) {
		char[] ch = {'J', 'a', 'v', 'a', ' ', 'P', 'r', 'o', 'g', 'r', 'a', 'm'};
		System.out.println(String.copyValueOf(ch, 1, 8));
		
		String str = "Hello " + String.copyValueOf(ch, 0, 4);
		System.out.println(str);
		
		String str1 = "Hello " + String.copyValueOf(ch, 5, 7);
		System.out.println(str1);
		
		String str2 = "Hello " + String.copyValueOf(ch, 0, 4) + " Programming";
		System.out.println(str2);
		
	}
}
Java copyValueOf Method 2

 It calls the public static String copyValueOf (char[] anCharArray, int Starting_Index, int Count) method. And it returns the string representation of the above-specified character array ch. It will start looking for a character at index position 1 (i.e., a) and count eight characters (i.e., a, v, a, , P, r, o, g). Then it will return the string representation of this subarray.

System.out.println(String.copyValueOf(ch, 1, 8));

Next, we are adding some string data to the method return value. It means Hello + character at index position 0 (i.e., J) and counts 4 characters (i.e., J, a, v, a)

String str = "Hello " + String.copyValueOf(ch, 0, 4);