How to convert String object to Boolean Object?

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.

Leave a Comment