java.lang.String valueOf(char[] data)

Description :

This java tutorial shows how to use the valueOf(char[] data) method of String class of java.lang package. This method returns a string representation of the character array data. It takes a copy of the character array data thus if the char array will be changed later, the value assigned as a result of calling this method will not be changed.

Method Syntax :

public static String valueOf(char[] data)

Parameter Input :

DataType Parameter Description
char[] data The character array method parameter we are interested to get the String equivalent

Method Returns :

The method valueOf(char[] data) returns a String object which is the String representation of char[] data.

Compatibility Version :

Since the beginning

Exception :


Discussion :

The java method, String valueOf(char[] data) should be accessed statically thus we should do the following String.valueOf(char[] data). This is basically one way to print a character array.

Java Code Example :

This java example source code demonstrates the use of valueOf(char[] data) method of String class. On this java code, we just declare a character array, and then we have printed the return String of method valueOf(). As you would noticed on the code, we have reinitialize the data character array into new values to prove our postulate that any change later on our array will not change the initial String value extracted in invoking valueOf. This is because we take a copy of the string equivalent of character once we invoked the method valueOf().

There’s an interesting method we have used, the String.toCharArray() to convert a String into character array.


* Example source code to show the use of static method valueOf(char[] data)

public class StringValueOfDemo {

	public static void main(String[] args) {
		// declare the character array
		char[] data = new char[]{'a','b','c','d'};
		// getting the String equivalent of char array data
		String strValue = String.valueOf(data);
		// printing the String equivalent of char array

		// change the value of char array
		// convert a string to char array
		data = "test".toCharArray();
		// check if the strValue has been changed by
		// reinitializing the data array
		System.out.println("After reinitialization: "+strValue);



Sample Output :

Running the valueOf() method example source code of java String class will give you the following output

java string indexOf-char-array method example

java string indexOf-char-array method example

Exception Scenario :

Not Applicable

Suggested Reading List :

References :