5

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.

4
  • 6
    Boolean.getBoolean does not do what you think it does. Read the manual. Commented Apr 25, 2012 at 10:41
  • 1
    I am not sure, but maybe it's case sensitive? By the way, why don't you use Boolean.parseBoolean(string);? Commented Apr 25, 2012 at 10:42
  • 3
    @PLB Guesses aren't much use, especially incorrect guesses such as this. OP should read the manual as advised, and so should you. Commented Apr 25, 2012 at 10:46
  • yes..it is not working.. Commented Apr 25, 2012 at 10:52

7 Answers 7

10

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".

Sign up to request clarification or add additional context in comments.

5 Comments

Using the built-ins is a better option than my solution. +1
if you are storing in a primitive, then parse is very, very slightly faster(as it avoids unboxing), but that time will be dwarfed by the parse time at any rate, so its a non-issue.
yes..it is returning the Boolean object of wrapper class. But not the boolean variable.
@mdhar9e Yes, but Java "unboxes" it, so it's hard to tell what it was.
Exactly you are right.This is the feature in java 1.5 . But we are using java 1.4.
8

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.

5 Comments

if strFlag contains false value?
then the 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.
Or just boolean boolFlag = "true".equalsIgnoreCase(strFlag);
thats just plain ugly, use the correct API for it like dasblinkenlight answered
I also promoted his answer. I Will change mine now, as this will probably be a place to look it up.
4

Boolean.getBoolean tests to see if the given system property is true, you want Boolean.parseBoolean

Comments

3

You can use

boolFlag = "true".equalsIgnoreCase(strFlag);

1 Comment

If strFlag would be null this would give you a NullPointerException.
3

Try to use Boolean.valueOf(strFlag)

Comments

3

Use Boolean.parseBoolean

boolean boolFlag = Boolean.parseBoolean(strFlag);

This method returns a boolean primitive type. It works the same as Boolean.valueOf, without the cost of unboxing.

Comments

1

You can use the following

Boolean.parseBoolean

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.