String.valueOf() vs. Object.toString()

后端 未结 10 752
挽巷
挽巷 2020-12-12 10:21

In Java, is there any difference between String.valueOf(Object) and Object.toString()? Is there a specific code convention for these?

相关标签:
10条回答
  • 2020-12-12 10:42

    The below shows the implementation for java.lang.String.valueOf as described in the source for jdk8u25. So as per my comment, there's no difference. It calls "Object.toString". For primitive types, it wraps it in its object form and calls "toString" on it.

    See below:

    /*
     * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
     * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
     */
    
    
        public static String valueOf(Object obj) {
            return (obj == null) ? "null" : obj.toString();
        }
    
        public static String valueOf(char data[]) {
            return new String(data);
        }
    
        public static String valueOf(char data[], int offset, int count) {
            return new String(data, offset, count);
        }
    
        public static String copyValueOf(char data[], int offset, int count) {
            return new String(data, offset, count);
        }
    
        public static String copyValueOf(char data[]) {
            return new String(data);
        }
    
        public static String valueOf(boolean b) {
            return b ? "true" : "false";
        }
    
        public static String valueOf(char c) {
            char data[] = {c};
            return new String(data, true);
        }
    
        public static String valueOf(int i) {
            return Integer.toString(i);
        }
    
        public static String valueOf(long l) {
            return Long.toString(l);
        }
    
        public static String valueOf(float f) {
            return Float.toString(f);
        }
    
        public static String valueOf(double d) {
            return Double.toString(d);
        }
    
    0 讨论(0)
  • 2020-12-12 10:44

    The most important difference is the way they handle null String references.

    String str = null;
    System.out.println("String.valueOf gives : " + String.valueOf(str));//Prints null
    System.out.println("String.toString gives : " + str.toString());//This would throw a NullPointerExeption
    
    0 讨论(0)
  • 2020-12-12 10:45

    String.valueOf(Object) and Object.toString() are literally the same thing.

    If you take a look at the implementation of String.valueOf(Object), you'll see that String.valueOf(Object) is basically just a null-safe invocation of toString() of the appropriate object:

    Returns the string representation of the Object argument.
    
    Parameters:
        obj an Object.
    Returns:
        if the argument is null, then a string equal to "null"; 
        otherwise, the value of obj.toString() is returned.
    See also:
        Object.toString()
    
    public static String valueOf(Object obj) {
        return (obj == null) ? "null" : obj.toString();
    }
    
    0 讨论(0)
  • 2020-12-12 10:46

    When argument is null, the String.valueOf returns "null", but Object.toString throws NullPointerException, that's the only difference.

    0 讨论(0)
  • 2020-12-12 10:46

    There is one more major difference between the two methods is when the object we are converting is an array.

    When you convert an array using Object.toString() you will get some kind of garbage value(@ followed by hashcode of array).

    To get a human-readable toString(), you must use String.valueOf(char[]); plz note that this method works only for Array of type char. I would recommend using Arrays.toString(Object[]) for converting arrays to String.

    Second difference is when the object is null, ValueOf() returns a String "null", while toString() will return null pointer exception.

    0 讨论(0)
  • 2020-12-12 10:50

    According to the Java documentation, String.valueOf() returns:

    if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.

    So there shouldn't really be a difference except for an additional method invocation.

    Also, in the case of Object#toString, if the instance is null, a NullPointerException will be thrown, so arguably, it's less safe.

    public static void main(String args[]) {  
        String str = null;
        System.out.println(String.valueOf(str));  // This will print a String equal to "null"        
        System.out.println(str.toString()); // This will throw a NullPointerException
    } 
    
    0 讨论(0)
提交回复
热议问题