valueOf():Convert boolean, char, double, float,int,long,object to String
static String valueOf(boolean b)
- Returns the string representation of the boolean argument.
static String valueOf(char c)
- Returns the string representation of the char argument.
static String valueOf(char[] data)
- Returns the string representation of the char array argument.
static String valueOf(char[] data, int offset, int count)
- Returns the string representation of a specific subarray of the char array argument.
static String valueOf(double d)
- Returns the string representation of the double argument.
static String valueOf(float f)
- Returns the string representation of the float argument.
static String valueOf(int i)
- Returns the string representation of the int argument.
static String valueOf(long l)
- Returns the string representation of the long argument.
static String valueOf(Object obj)
- Returns the string representation of the Object argument.
public class Main {
public static void main(String[] argv) {
String str = String.valueOf(123);
System.out.println(str);
}
}
The output:
123
public class Main {
public static void main(String args[]) {
char charArray[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
boolean booleanValue = true;
char characterValue = 'Z';
int integerValue = 7;
long longValue = 10000000000L; // L suffix indicates long
float floatValue = 2.5f; // f indicates that 2.5 is a float
double doubleValue = 3.3; // no suffix, double is default
Object objectRef = "hello"; // assign string to an Object reference
System.out.printf("char array = %s\n", String.valueOf(charArray));
System.out.printf("part of char array = %s\n", String.valueOf(charArray, 3, 3));
System.out.printf("boolean = %s\n", String.valueOf(booleanValue));
System.out.printf("char = %s\n", String.valueOf(characterValue));
System.out.printf("int = %s\n", String.valueOf(integerValue));
System.out.printf("long = %s\n", String.valueOf(longValue));
System.out.printf("float = %s\n", String.valueOf(floatValue));
System.out.printf("double = %s\n", String.valueOf(doubleValue));
System.out.printf("Object = %s\n", String.valueOf(objectRef));
}
}
Home
Java Book
Essential Classes
Java Book
Essential Classes
String:
- String type and Literals
- String Concatenation
- String.CASE_INSENSITIVE_ORDER
- String Constructor
- charAt(int index):Get a single char by index
- String: compareTo(String stringValue)
- concat(String str)
- equals():Compare two string value for equality
- equals( ) vs ==
- contains(CharSequence s)
- copyValueOf(char[] data)
- endsWith(String suffix)
- format():Format a string
- getBytes():Get byte array from string
- getChars()
- indexOf
- intern a string
- isEmpty:if string is empty
- lastIndexOf()
- length() Returns the length of this string
- startsWith( )
- toLowerCase() and toUpperCase(): convert string case with locale
- substring:Get sub string from a string
- toCharArray():Get char array from string
- toString( )
- trim()
- valueOf():Convert boolean, char, double, float,int,long,object to String