When i tried to convert a String Object to boolean, the result is different.
String strFlag="true";
boolean boolFlag = Boolean.getBoolean(strFlag);
boolFlag ends up having a false value.
Use Boolean.valueOf
boolean boolFlag = Boolean.valueOf(strFlag);
This method returns a Boolean with a value represented by the specified String. The Boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".
Boolean object of wrapper class. But not the boolean variable.Use Boolean.valueOf(String string) to archieve your goal.
boolean boolFlag = Boolean.valueOf(strFlag);
Returns a Boolean with a value represented by the specified String. The Boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".
Example: Boolean.valueOf("True") returns true.
Example: Boolean.valueOf("yes") returns false.
As of java 1.5 there's also Boolean.parseBoolean(String s) which returns the primitive type boolean instead of the boxed type Boolean to spare some CPU cycles in most cases.
strFlag contains false value?boolFlag is false. Since the boolean can only be true or false, it suffices to check for one of them, depending on what the "default" should be. For the wrapper Boolean you would be right, since that one could become null.boolean boolFlag = "true".equalsIgnoreCase(strFlag);You can use
boolFlag = "true".equalsIgnoreCase(strFlag);
null this would give you a NullPointerException.boolean boolFlag = Boolean.parseBoolean(strFlag);
This method returns a boolean primitive type. It works the same as Boolean.valueOf, without the cost of unboxing.
Boolean.parseBoolean(string);?