If you are sure that your value is not null , you can use the third option, which
String str3 = b.toString();
and his code looks like
public String toString() { return value ? "true" : "false"; }
If you want to have null use String.valueOf(b) whose code looks like
public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); }
as you will see that it will check null first and then call the toString() method on your object.
A call to Boolean.toString(b) calls
public static String toString(boolean b) { return b ? "true" : "false"; }
which is slightly slower than b.toString() , because the JVM needs to first unpack the Boolean into a Boolean , which will be passed as an argument to Boolean.toString(...) , and b.toString() reuse the private boolean value field in the Boolean object, which has its own condition.
Pshemo Sep 16 '13 at 16:37 2013-09-16 16:37
source share