How to compare Boolean?

后端 未结 7 1450
太阳男子
太阳男子 2021-01-01 09:39

Take this for example (excerpt from Java regex checker not working):

while(!checker) {
    matcher = pattern.matcher(number);
    if(matcher.find())
                 


        
相关标签:
7条回答
  • 2021-01-01 09:56

    Using direct conditions (like ==, !=, !condition) will have a slight performance improvement over the .equals(condition) as in one case you are calling the method from an object whereas direct comparisons are performed directly.

    0 讨论(0)
  • 2021-01-01 09:56

    .equals(false) will be slower because you are calling a virtual method on an object rather than using faster syntax and rather unexpected by most of the programmers because code standards that are generally used don't really assume you should be doing that check via .equals(false) method.

    0 讨论(0)
  • 2021-01-01 10:01

    As object?

    equals

    public boolean equals(Object obj)

    Returns true if and only if the argument is not null and is a Boolean object that represents the same boolean value as this object.

    Overrides: equals in class Object

    Parameters: obj - the object to compare with.

    Returns: true if the Boolean objects represent the same value; false otherwise.

    boolean a = true;
    boolean b = false;
    
    System.out.println("a.equals(b):" + ((Object)a).equals( ((Object)b) ));
    

    Output: a.equals(b):false

    0 讨论(0)
  • 2021-01-01 10:07

    As long as checker is not null, you may use !checker as posted. This is possible since Java 5, because this Boolean variable will be autoboxed to the primivite boolean value.

    0 讨论(0)
  • 2021-01-01 10:11

    Try this:

    if (Boolean.TRUE.equals(yourValue)) { ... }
    

    As additional benefit this is null-safe.

    0 讨论(0)
  • 2021-01-01 10:15

    From your comments, it seems like you're looking for "best practices" for the use of the Boolean wrapper class. But there really aren't any best practices, because it's a bad idea to use this class to begin with. The only reason to use the object wrapper is in cases where you absolutely must (such as when using Generics, i.e., storing a boolean in a HashMap<String, Boolean> or the like). Using the object wrapper has no upsides and a lot of downsides, most notably that it opens you up to NullPointerExceptions.

    Does it matter if '!' is used instead of .equals() for Boolean?

    Both techniques will be susceptible to a NullPointerException, so it doesn't matter in that regard. In the first scenario, the Boolean will be unboxed into its respective boolean value and compared as normal. In the second scenario, you are invoking a method from the Boolean class, which is the following:

    public boolean equals(Object obj) {
        if (obj instanceof Boolean) {
            return value == ((Boolean)obj).booleanValue();
        }
        return false;
    }
    

    Either way, the results are the same.

    Would it matter if .equals(false) was used to check for the value of the Boolean checker?

    Per above, no.

    Secondary question: Should Boolean be dealt differently than boolean?

    If you absolutely must use the Boolean class, always check for null before performing any comparisons. e.g.,

    Map<String, Boolean> map = new HashMap<String, Boolean>();
    //...stuff to populate the Map
    Boolean value = map.get("someKey");
    if(value != null && value) {
        //do stuff
    }
    

    This will work because Java short-circuits conditional evaluations. You can also use the ternary operator.

    boolean easyToUseValue = value != null ? value : false;
    

    But seriously... just use the primitive type, unless you're forced not to.

    0 讨论(0)
提交回复
热议问题