Java String valueOf() method
Java String valueOf() method converts different types of values into String.
Such as : int to String, long to String, boolean to String, character to String, float to String, double to String, object to String and char array to String.
Syntax:
public static String valueOf(boolean b) public static String valueOf(char c) public static String valueOf(char[] c) public static String valueOf(int i) public static String valueOf(long l) public static String valueOf(float f) public static String valueOf(double d) public static String valueOf(Object o)
Returns:
It returns String, representation of given value.
Java String valueOf(char c) method example
public class JavaStringValueOfEx1
{
public static void main(String[] args)
{
// declare a sample string value
String strValue = "=ROY|N.K.=";
// convert strValue to character array
char[] values = strValue.toCharArray();
// for each element on char array
for(char c:values){
// print the String value of char c
System.out.println(String.valueOf(c));
}
}
}
Output:
= R O Y | N . K . =
Java String valueOf() method example 2
public class JavaStringValueOfEx2
{
public static void main(String args[])
{
int value=10;
String s1=String.valueOf(value);
System.out.println(s1+10);//concatenating string with 10
}
}
Output:
1010
Java String valueOf(boolean bol) method example 3
public class JavaStringValueOfEx3
{
public static void main(String[] args)
{
// Boolean to String
boolean bol = true;
boolean bol2 = false;
String s1 = String.valueOf(bol);
String s2 = String.valueOf(bol2);
System.out.println(s1);
System.out.println(s2);
}
}
Output:
true false
Java String valueOf(float f) and valueOf(double d) Example 4
public class JavaStringValueOfEx4 {
public static void main(String[] args) {
// Float and Double to String
float f = 15.45f;
double d = 15.32;
String s1 = String.valueOf(f);
String s2 = String.valueOf(d);
System.out.println(s1);
System.out.println(s2);
}
}
Output:
15.45 15.32
Java String valueOf Example 5
public class JavaStringValueOfEx5 {
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[]={'t','u','t','o','r','i','a','l','a','n','d','e','x','a','m','p','l','e'};
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);
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);
}
}
Output:
true 11 12 13 14 15.5 16.5 tutorialandexample
