What's the difference between using Object.toString () and String.valueOf ()?

18

I was doing some tests and realized that using one or the other, the result ends up being the same. I made this code to exemplify:

public class ParseToStringTest {

    public static void main(String[] args) {
        new ParseToStringTest().parseTest();
    }

    public void parseTest() {

        SomeObj o = new SomeObj();
        System.out.println(o.toString());
        System.out.println(String.valueOf(o));

    }

    class SomeObj {}
}

Running on ideone , the result I got was:

[email protected]
[email protected]

That is, the result was identical.

Would it make any difference between using String#valueOf() or Object#toString() to display a representation of the object in the form of a String?

    
asked by anonymous 29.05.2017 / 16:49

3 answers

11

At documentation says " if the argument is null it is returned null , otherwise the value returns .toString ". According to this lick, String.valueOf(Object) prints null while Object.toString() can generate NullPointerException .

Another important detail is that valueOf is a static method, whereas toString is an instance method and can only be called in a reference type. However, valueOf maybe, I say maybe, be more flexible compared to toString() . Here are the basic types of static method valueOf , which can be invoked:

String   valueOf(boolean b) 
String   valueOf(char c) 
String   valueOf(char[] data) 
String   valueOf(char[] data, int offset, int count) 
String   valueOf(double d) 
String   valueOf(float f) 
String   valueOf(int i) 
String   valueOf(long l) 
String   valueOf(Object obj) 

If you are dealing with toString() , you will always need to call your specific class. Here's an example:

Integer.toString(int i)   // inteiros
Float.toString(float f);  // flutuantes

See these examples:

Example 1:

public String doIt(float number) {

  return String.valueOf(number);
}

Example 2:

public String doIt(float number) {

  return Float.toString(number);
}
    
29.05.2017 / 17:23
15

Oracle

  

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

Translation:

  

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

Translated into code would be:

public static String valueOf(Object obj) {
    return (obj == null) ? "null" : obj.toString();
}

In the case of a Object#toString , if the instance is null a NullPointerException will be released. There should not be a difference except for an additional method invocation, meaning they are pretty much the same thing.

    
29.05.2017 / 16:56
12

Almost none. There is an additional indirection in String.valueOf , which is checking whether the parameter is null or not.

Paraphrasing the String.valueOf documentation % :

The method will check if the object is null and, if it is, it is returned to string "null" , otherwise the object will be called .toString() .

In code, valueOf is basically:

public static String valueOf(Object obj) {
    return obj == null ? "null" : obj.toString();
}

There are still classes that implement a static version of toString() (such as Integer " or Arrays ", for example), this mainly serves for these classes to work on an alternative version of the object representation as string , allowing additional parameters or a custom implementation independent of the original .toString() .

This is very clear in the Integer class, where there is an overload that can define which base the number will be shown.

Integer number = 15;

System.out.println(Integer.toString(number, 10)); // 15   (decimal, base 10)
System.out.println(Integer.toString(number, 2));  // 1111 (binário, base 2)
System.out.println(Integer.toString(number, 16)); // f    (hexadecimal, base 16)

See this example working on repl.it.

Then, it is best to use String.valueOf when the object can be null, in other cases, you can use the .toString() .

    
29.05.2017 / 16:56