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  

String:
  1. String type and Literals
  2. String Concatenation
  3. String.CASE_INSENSITIVE_ORDER
  4. String Constructor
  5. charAt(int index):Get a single char by index
  6. String: compareTo(String stringValue)
  7. concat(String str)
  8. equals():Compare two string value for equality
  9. equals( ) vs ==
  10. contains(CharSequence s)
  11. copyValueOf(char[] data)
  12. endsWith(String suffix)
  13. format():Format a string
  14. getBytes():Get byte array from string
  15. getChars()
  16. indexOf
  17. intern a string
  18. isEmpty:if string is empty
  19. lastIndexOf()
  20. length() Returns the length of this string
  21. startsWith( )
  22. toLowerCase() and toUpperCase(): convert string case with locale
  23. substring:Get sub string from a string
  24. toCharArray():Get char array from string
  25. toString( )
  26. trim()
  27. valueOf():Convert boolean, char, double, float,int,long,object to String