20
I was doing some tests and I realized that using one or the other, the result turns out to be 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 {}
}
Rotating in the ideone, the result I got was:
Ideone$SomeObj@677327b6
Ideone$SomeObj@677327b6
That is, the result was identical.
There would be some difference between using String#valueOf()
or Object#toString()
to display a String-shaped object representation?
https://books.google.com/books?id=CsGtipt1wsQC&pg=PA330&lpg=PA330&dq=diferen%C3%A7a+entre+usar+Object.toString()+e+String.valueOf()? &source=bl&ots=ow7LMsxLf7&Sig=puZzLbqAuaKAguO3dzstM0dZGY&hl=en-Br&sa=X&ved=0ahUKEwjX_IGxrZXUAhVKOZAKHWFXA38Q6AEITTAG#v=onepage&q=difference%C3%A7a%20entre%20usar%20Object.toString()%20e%20String.valueOf()%3F&f=false
– user60252
The
.toString()
NPE spear,String.valueOf()
nay– Jefferson Quesado
Also the
String.valueOf()
check for nulls, so it may have some overhead in the application– Jefferson Quesado