Java String valueOf Method

The Java String valueOf Method is one of the String Methods to return the string representation of the user-specified value.

This article will show the string representation of different data types using the Java valueOf method with an example. The syntax of the String.valueOf in Java Programming language is

Java String valueOf Method syntax

The Java Programming Language provides nine different methods to return the String representation. The following Java valueof function will accept the Boolean value as the parameter and returns the string representation of it.

public static String valueOf(Boolean bool); 

//In order to use in program
String.valueOf(Boolean bool);

It accepts the Character as the parameter and returns the char value in string representation.

public static String valueOf(char ch); 

//In order to use in program
String.valueOf(char ch);

This Java valueOf function will accept the Character Array as the parameter. Next, it returns the string representation of the Char array.

public static String valueOf(char[] anCharArray); 

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

It will accept the Character Array as the first parameter, Starting index position (Starting_index) as the second argument, and the array counts as the third argument. The Java valueOf method will start representing from the Starting_index and return to the user-specified count.

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

//In order to use in program
String.valueOf(char[] anCharArray, int Starting_index, int count)

The following Java valueOf function will accept the Integer value as the parameter & return the string representation of the Integer value.

public static String valueOf(int integer); 

//In order to use in program
String.valueOf(int integer);

This Java Programming method will accept the Long value as the parameter and returns the string representation of the Long value.

public static String valueOf(long l); 

//In order to use in program
String.valueOf(long l);

This method will accept the Double value as the parameter and returns the string representation of the Double value.

public static String valueOf(double dbl); 

//In order to use in program
String.valueOf(double dbl);

This String function accepts the Float value as the parameter and returns the string representation of the Float value.

public static String valueOf(float fl); 

//In order to use in program
String.valueOf(float fl);

The following Java valueOf method will accept the Object value as the parameter and returns the string representation of an Object.

public static String valueOf(Object obj); 

//In order to use in program
String.valueOf(Object obj);

Return Value: It accepts different types of arguments and returns the String representation of the user-specified argument value.

Java String valueOf Method example

In this Java program, we will declare variables of different data types. Next, we used the Java valueOf method to return the string representation of declared variables.

package StringFunctions;

public class ValueOfMethod {
	public static void main(String[] args) {
		boolean bool = true;
		char c = 'T';
		char[] ch = {'J', 'a', 'v', 'a', ' ', 'P', 'r', 'o', 'g', 'r', 'a', 'm'};
		int i = 99999;
		long l = 987654321;
		float flot = 10.25f;
		double dbl = 189.25987;
		String str = "India";
		
		System.out.println("String Representation of Boolean = " + String.valueOf(bool));
		System.out.println("String Representation of Character = " + String.valueOf(c));
		System.out.println("String Representation of Character Array = " + String.valueOf(ch));
		System.out.println("String Representation of Character Array = " + String.valueOf(ch, 5, 7));
		System.out.println("String Representation of Integer = " + String.valueOf(i));
		System.out.println("String Representation of Long = " + String.valueOf(l));
		System.out.println("String Representation of Float = " + String.valueOf(flot));
		System.out.println("String Representation of Double = " + String.valueOf(dbl));
		System.out.println("String Representation of an Object = " + String.valueOf(str));	
	}
}
Java String valueOf Method 1

Within this Java valueOf example, we declared the variable of available data types and assigned some random values.

boolean bool = true;
char c = 'T';
char[] ch = {'J', 'a', 'v', 'a', ' ', 'P', 'r', 'o', 'g', 'r', 'a', 'm'};
int i = 99999;
long l = 987654321;
float flot = 10.25f;
double dbl = 189.25987;
String str = "India";

The following System.out.println statement will call the valueOf (boolean bool) method to return the string representation of Boolean value bool.

System.out.println("String Representation of Boolean = " + String.valueOf(bool));

The following System.out.println statement will call the valueOf (char c) method. And return the string representation of Character c.

System.out.println("String Representation of Character = " + String.valueOf(c));

This Java statement will call the valueOf (char[] ch) method to return the character array ch string representation.

System.out.println("String Representation of Character Array = " + String.valueOf(ch));

The following statement calls the valueOf (char[] ch, int Starting_Index, int count) method to return the character array ch string representation. It will start looking for the character at index position 5 (i.e., P) and count seven characters (i.e., P, r, o, g, r, a, m). Then it will return the string representation of this subarray.

System.out.println("String Representation of Character Array = " + String.valueOf(ch, 5, 7));

The below statement will call the valueOf (int i) method to return a string representation of integer value i.

System.out.println("String Representation of Integer = " + String.valueOf(i));

It will call the (long l) method to return the string representation of Long value l.

System.out.println("String Representation of Long = " + String.valueOf(l));

It will call the Java valueOf (float fl) method to return the string value of Float value flot.

System.out.println("String Representation of Float = " + String.valueOf(flot));

The following statement will call the (double dbl) method to output the string representation of Double value dbl.

System.out.println("String Representation of Double = " + String.valueOf(dbl));

The following statement calls the (Object obj) method to return String value str.

System.out.println("String Representation of an Object = " + String.valueOf(str));