If you decompile these examples using javap -c you will see that in first case
StringBuilder will be created
- and it will
append(Object obj)
- which internally will invoke
append(String.valueOf(obj));
- which first will check for
null reference.
So main difference in result of null
Object o = null;
System.out.println(o + ""); // result "null"
System.out.println(o.toString()); // throws NPE
If your data can't be null or you want to throw NPE you should use toString() for better performance.
If your data can be null and in this case result string should also be "null" String.valueOf(object) will give you better performance than object+"".