How to convert String object to Boolean Object?

Suresh Chaganti picture Suresh Chaganti · Oct 8, 2009 · Viewed 564.8k times · Source

How to convert String object to Boolean object?

Answer

KLE picture KLE · Oct 8, 2009

Try (depending on what result type you want):

Boolean boolean1 = Boolean.valueOf("true");
boolean boolean2 = Boolean.parseBoolean("true");

Advantage:

  • Boolean: this does not create new instances of Boolean, so performance is better (and less garbage-collection). It reuses the two instances of either Boolean.TRUE or Boolean.FALSE.
  • boolean: no instance is needed, you use the primitive type.

The official documentation is in the Javadoc.


UPDATED:

Autoboxing could also be used, but it has a performance cost.
I suggest to use it only when you would have to cast yourself, not when the cast is avoidable.