Best approach to convert Boolean object to string in java

I am trying to convert boolean to a string type ...

Boolean b = true; String str = String.valueOf(b); 

or

 Boolean b = true; String str = Boolean.toString(b); 

which would be more efficient?

+76
java boolean
Sep 16 '13 at 16:35
source share
6 answers

I do not think that between them there would be a significant difference in performance, but I would prefer the first option.

If you have a Boolean link, Boolean.toString(boolean) will throw a NullPointerException if your link is null . Since this link is disabled before Boolean before passing to the method.

So far, the String.valueOf() method, as the source code shows, does the explicit null check:

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

Just check this code:

 Boolean b = null; System.out.println(String.valueOf(b)); // Prints null System.out.println(Boolean.toString(b)); // Throws NPE 



There is no difference for a primitive boolean.

+102
Sep 16 '13 at 16:39
source share

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.

+19
Sep 16 '13 at 16:37
source share
 public class Sandbox { /** * @param args the command line arguments */ public static void main(String[] args) { Boolean b = true; boolean z = false; echo (b); echo (z); echo ("Value of b= " + b +"\nValue of z= " + z); } public static void echo(Object obj){ System.out.println(obj); } } 
 Result -------------- true false Value of b= true Value of z= false -------------- 
+3
Jan 04 '15 at 12:55
source share

If this is intended to provide a constant "true" value, rather than a "true" or "true" value, you can use this:

 Boolean.TRUE.toString(); Boolean.FALSE.toString(); 
+3
Jul 01 '16 at 17:00
source share

Depends on what you mean by "effective." The performance of both versions matches the same bytecode.

 $ ./javap.exe -c java.lang.String | grep -A 10 "valueOf(boolean)" public static java.lang.String valueOf(boolean); Code: 0: iload_0 1: ifeq 9 4: ldc #14 // String true 6: goto 11 9: ldc #10 // String false 11: areturn $ ./javap.exe -c java.lang.Boolean | grep -A 10 "toString(boolean)" public static java.lang.String toString(boolean); Code: 0: iload_0 1: ifeq 9 4: ldc #3 // String true 6: goto 11 9: ldc #2 // String false 11: areturn 
+1
Sep 26 '16 at 13:02
source share

If you are looking for a quick way to do this, such as debugging, you can simply concatenate an empty string to a boolean:

 System.out.println(b+""); 

However, I highly recommend using a different method for use in production. This is a simple quick fix that is useful for debugging.

0
Apr 21 '15 at 12:08
source share



All Articles