How to get Boolean value from Object

后端 未结 4 718
盖世英雄少女心
盖世英雄少女心 2021-01-12 17:42

I tried different ways to fix this, but I am not able to fix it. I am trying to get the Boolean value of an Object passed inside this method of a checkBox:<

4条回答
  •  广开言路
    2021-01-12 18:20

    Here's some of the source code for the Boolean class in java.

    // Boolean Constructor for String types.
    public Boolean(String s) {
        this(toBoolean(s));
    }
    // parser.
    public static boolean parseBoolean(String s) {
        return toBoolean(s);
    }
    // ...
    // Here's the source for toBoolean.
    // ...
    private static boolean toBoolean(String name) { 
        return ((name != null) && name.equalsIgnoreCase("true"));
    }
    

    So as you can see, you need to pass a string with the value of "true" in order for the boolean value to be true. Otherwise it's false.

    assert new Boolean( "ok" ) == false; 
    assert new Boolean( "True" ) == true;
    assert new Boolean( "false" ) == false;
    
    assert Boolean.parseBoolean( "ok" ) == false; 
    assert Boolean.parseBoolean( "True" ) == true;
    assert Boolean.parseBoolean( "false" ) == false;
    

提交回复
热议问题