Java Tip #1. Stop to use "toString" method!
When you need to get string value of some object you should use String.valueOf method.
The String.valueOf method is null-safe and more clear way to get string value of the target object.
class SomeClass {
@Override public String toString() {
return "String value of SomeClass instance!"
}
}
public class Main {
public static void main(String[] args) {
SomeClass someClassInstance = new SomeClass();
System.out.println(String.valueOf(someClassInstance));
}
}
Written by Maks Atygaev
Related protips
3 Responses

Well done!!! You awesome =)
over 1 year ago
·

Nice tip! thanks...
over 1 year ago
·

remember this
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#String
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#