0

I would like to convert my boolean object to String. Which among the following would be the better /right way of doing it and why?

boolean booleanValue = true;
String strValue = Boolean.toString(booleanValue);

or

boolean booleanValue = true;
String strValue = booleanValue + "";

EDIT

The below answers prompts me to add few more

boolean booleanValue = true;
String strValue = String.valueOf(booleanValue);

and for non-primitive data types like Integer

Integer i = 10;
String strValue = i.toString();

So, finally we have three different types Boolean.toString() , concat with + , String.valueOf() to convert any data type to String

4 Answers4

1

Use String.valueOf(booleanValue) or Boolean.toString(booleanValue) as they're both concise and convey clearly what you're trying to do. Avoid concatenating Strings with + "" as it's slightly slower (although negligibly) and doesn't convey meaning as well as the other two options.

Jake Miller
  • 2,432
  • 2
  • 24
  • 39
  • Thanks for your time to explain me. But I would like to know why concatenating Strings with `+""` makes it slightly slower ? –  Apr 26 '17 at 05:37
  • The compiler typically generates a `StringBuffer` and calls the `.append()` method to generate your string which leads to it being slower than the other options. Your 2nd option would be as follows: `new StringBuffer().append(booleanValue).append("").toString()` – Jake Miller Apr 26 '17 at 05:55
0

The former can be executed faster, while the latter takes less screen space.

By the way, in the user interface of your program, ordinary humans should never see the words true/false, so a third way would be (booleanValue ? "yes" : "no"), translated to the appropriate language. Or on/off or enabled/disabled or visible/hidden, etc.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121
  • Illing : Yeah that's the nice way of doing. But I would like to know how the `former one would be executed faster` ? –  Apr 26 '17 at 05:38
  • It seems `+` operator works internally with the help of `StringBuilder` so it makes a bit slower than the other one. Reference : http://stackoverflow.com/a/5464427/6680585 –  Apr 26 '17 at 06:43
0

Giving the following example:

// One
String value = Integer.toString(someValue);

// Two
String value = someValue + "";

// let me add Three 
String value = String.valueOf(someValue);

I will recommend the usage of String.valueOf(). The reason for that is that this call does not explicitly contain the type of the argument, so if later is changed it from int to double or boolean, there is no need to modify the code.

The second I will put it solution is out, because implicitly creates a StringBuilder and appends the components.

I guessing that #1 compared to #3 is only a little faster, so I stay in 3.

dsapandora
  • 104
  • 7
0

Among the two options you have mentioned, go for the first one for Production use.

Use the second option only for debugging. For example you want to apply print statement that shows you the output of any particular method.

Other than above two options you can go for String.valueOf(boolean_obj) method. Advantage being if the boolean object has null assigned to it, it will get assigned as null string where as when using Boolean.toString(boolean_obj) it will throw a NullPointerException.

Dhaval Simaria
  • 1,886
  • 3
  • 28
  • 36