Java String valueOf Method

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

In this article, we will show the string representation of different data types using Java String.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 string valueof method will accept the Boolean value as the parameter and returns the string representation of Boolean value.

public static String valueOf(Boolean bool); // It will return String

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

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

public static String valueOf(char ch); // It will return String

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

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

public static String valueOf(char[] anCharArray); // It will return String

//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 count as the third argument. String.valueOf method will start representing from the Starting_index and return up to user-specified count.

public static String valueOf(char[] anCharArray, int Starting_index, int count); // It will return String

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

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

public static String valueOf(int integer); // It will return String

//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 Long value.

public static String valueOf(long l); // It will return String

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

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

public static String valueOf(double dbl); // It will return String

//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 Float value.

public static String valueOf(float fl); // It will return String

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

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

public static String valueOf(Object obj); // It will return String

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

Return Value: The String.valueOf Method 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 String valueOf method to return the string representation of declared variables.

// Java String.valueOf Method example
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 String 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 public static String 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 public static String valueOf (char c) method. And return the string representation of Character c.

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

This Java string Valueof statement will call the public static String valueOf (char[] ch) method to return the string representation of Character array ch.

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

The following statement calls the public static String valueOf (char[] ch, int Starting_Index, int count) method to return the string representation of Character array ch. 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));

Below string valueof statement will call the public static String 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 public static String valueOf (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 public static String 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 Java String valueOf statement will call the public static String valueOf (double dbl) method to output the string representation of Double value dbl.

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

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

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