Which has best performance while converting String to boolean from the below options.
boolean value = new Boolean(\"true\").booleanValue();
boolean value = new Boolean("true").booleanValue();
Worst, creates new Boolean
objects all the time. BTW booleanValue()
is not necessary, unboxing will do it for you.
boolean value = Boolean.valueOf("true");
Much better, uses cached Boolean
instance, but performs unnecessary (although very cheap) unboxing.
boolean value = Boolean.parseBoolean("true");
Best, nothing is wasted, operates barely on primitives, no memory allocations involved. BTW all of them delegate to (Sun/Oracle):
private static boolean toBoolean(String name) {
return ((name != null) && name.equalsIgnoreCase("true"));
}
If you are paranoid you can create your own toBoolean(String name)
not ignoring case - negligibly faster:
boolean value = "true".equals(yourString);