The java string valueOf method is used to convert different types of values into a string. Using this method you can convert Boolean to a string, char to a string, char array to a string, double to a string, integer to string, float to a string, long to a string, and short into a string.
Java valueOf Example
- Input:String.valueOf(4)
Output: "4" //converted a integer data type value into string.
- Input:String.valueOf(4.5d)
Output:"4.5" //converted a double data type value into string.
- Input:String.valueOf(12.8L)
Output:"12.8" //converted a long data type value into string.
- Input:String.valueOf(9.8f)
Output:"9.8" //converted a float data type value into string.
- Input:String.valueOf('A')
Output:"A" //converted a character data type value into string.
- Input:String.valueOf(true)
Output:"true" //converted a boolean data type value into string.
Java valueOf Syntax
String.valueOf(boolean b)
String.valueOf(char c)
String.valueOf(char[] data)
String.valueOf(double d)
String.valueOf(float f)
String.valueOf(int b)
String.valueOf(long l)
String.valueOf(short s)
Java valueOf() Parameters
datatype variable - we need to input a variable with it's data type that needs to be change into a string.
Java valueOf() Return
This method returns a converted string (variable with any data type into a string).
Java valueOf() Program
package codesdope;
import java.util.*;
public class codesdope
{
public static void main(String[] args)
{
boolean b1=true;
byte b2=11;
short sh = 12;
int i = 13;
long l = 14L;
float f = 15.5f;
double d = 16.5d;
char chr[]={'j','a','v','a'};
System.out.println(String.valueOf(b1));
System.out.println(String.valueOf(b2));
System.out.println(String.valueOf(sh));
System.out.println(String.valueOf(i));
System.out.println(String.valueOf(l));
System.out.println(String.valueOf(f));
System.out.println(String.valueOf(d));
System.out.println(String.valueOf(chr));
}
}
true
11
12
13
14
15.5
16.5
java
In the above program, we have applied the valueOf() method in multiple cases like Boolean, byte, integer, short, long, float, char, char array. And in all cases, it returned the string value of all these data types.
How to check that our value is converted into a string using the valueOf() method?
- If we will simply add 7 in it then it will give an output of 10.
- But, if we will first convert the integer value 3 into String using the
valueOf()
method then add 7 in it then it should give an output of 37 because strings concatenate.
package codesdope;
import java.util.*;
public class codesdope
{
public static void main(String[] args) {
int a=45;
System.out.print("Addition before converting:");
System.out.println(a+5);
System.out.print("Addition after converting:");
System.out.println(String.valueOf(a)+5);
}
}
Addition before converting:50
Addition after converting:455
- When we added 5 with the given variable i.e 45 then our output is 50.
- But, when we have converted our variable into a string using
valueOf()
method then our output is 455 because it behaved like a string and got concatenated.