valueOf() method
This method will allow you to convert the different value types into the string.
Method implementation-
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
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)
Example-
public class Simple{
public static void main(String[] args) {
int val=20;
String s2=String.valueOf(val);
System.out.println(s2);
}
}
Output-
Example-
public class Simple{
public static void main(String[] args) {
int val=20;
String s=String.valueOf(val);
System.out.println(s);
boolean b=true;
String s3=String.valueOf(b);
System.out.println(s3);
char c='A';
String s4=String.valueOf(c);
System.out.println(s4);
float f = 10.05f;
double d = 10.02;
String s1 = String.valueOf(f);
String s2 = String.valueOf(d);
System.out.println(s1);
System.out.println(s2);
}
}
Output-