Java String valueOf()

Java allows you to convert values from one type to another, given certain conditions. We can convert a string/text variable to a numeric value, but only if the string value is numeric. When converting from a numeric to a string value, however, we have a little more leeway. This is because a String can contain both numeric and text values.

ValueOf Method

We will use the valueOf method in Java to convert an int to a String. This method is actually built within the Java String class. Recall that Java not only lets you declare primitive data types (int, double, float), but it stores those in classes: Integer, Double, and String. The valueOf method accepts one parameter, the integer value we are converting.

Java String valueOf() Method Signature

Below image shows the valueOf() overloaded methods, they all are public and static and returns String.

String.valueOf(Object obj)
public class JavaStringValueOfExamples { public static void main(String[] args) { Data obj = new Data(10); String str = String.valueOf(obj); System.out.println(str); } } class Data{ private int id; public Data(int i) { = i; } @Override public String toString() { return "Data{id:"+id+"}"; } }

Note: that toString() method is used to create the String representation of the Object.

String.valueOf(char[] data)
char[] chars = {'a', 'b', 'c'}; String str1 = String.valueOf(chars); System.out.println(str1);
String.valueOf(char[] data, int offset, int count)
char[] chars = {'a', 'b', 'c'}; String str2 = String.valueOf(chars,1,2); System.out.println(str2);
String.valueOf(boolean b)
boolean flag = true; String str3 = String.valueOf(flag); System.out.println(str3);
String.valueOf(char c)
char c = 65; // same as char c = 'A' String str4 = String.valueOf(c); System.out.println(str4);
String.valueOf(int i)
int i = 'A'; // same as int i = 65 String str5 = String.valueOf(i); System.out.println(str5);
String.valueOf(long l)
long l = 12345L; String str6 = String.valueOf(l); System.out.println(str6);
String.valueOf(float f)
float f = 123.45f; String str7 = String.valueOf(f); System.out.println(str7);
String.valueOf(double d)
double d = 123.45d; String str8 = String.valueOf(d); System.out.println(str8);
Java String valueOf() method example
public class Test { public static void main(String args[]) { int value=31; String p=String.valueOf(value); System.out.println(p+36); } }

Java String valueOf(boolean bol) Method Example

This is a boolean version of overloaded valueOf() method. It takes boolean value and returns a string.

public class Test { public static void main(String[] args) { boolean x = true; boolean y = false; String p = String.valueOf(x); String q = String.valueOf(y); System.out.println(p); System.out.println(q); } }

true false
Java String valueOf(char ch) Method Example

This is a char version of overloaded valueOf() method. It takes char value and returns a string.

public class Test { public static void main(String[] args) { char char1 = 'P'; char char2 = 'R'; String p = String.valueOf(char1); String q = String.valueOf(char2); System.out.println(p); System.out.println(q); } }

Java String valueOf(float f) and valueOf(double d)

This is a float version of overloaded valueOf() method. It takes float value and returns a string.

public class Test { public static void main(String[] args) { float f = 15.07f; double d = 14.01; String p = String.valueOf(f); String q = String.valueOf(d); System.out.println(p); System.out.println(q); } }

15.07 14.01
Java String valueOf() Complete Examples

See an example where we are converting all primitives and objects into strings.

public class Test { public static void main(String[] args) { boolean b1=true; byte b2=31; short sh = 36; int i = 14; long l = 20L; float f = 16.5f; double d = 18.5d; char chr[]={'R','a','m','a'}; Test obj=new Test(); String s1 = String.valueOf(b1); String s2 = String.valueOf(b2); String s3 = String.valueOf(sh); String s4 = String.valueOf(i); String s5 = String.valueOf(l); String s6 = String.valueOf(f); String s7 = String.valueOf(d); String s8 = String.valueOf(chr); String s9 = String.valueOf(obj); System.out.println(s1); System.out.println(s2); System.out.println(s3); System.out.println(s4); System.out.println(s5); System.out.println(s6); System.out.println(s7); System.out.println(s8); System.out.println(s9); } }

true 31 36 14 20 16.5 18.5 Rama Test@3e25a5