Boolean.TRUE == myBoolean vs. Boolean.TRUE.equals(myBoolean)

后端 未结 4 1331
执笔经年
执笔经年 2021-02-08 08:51

Is there ever a situation where using equals(Boolean) and == would return different results when dealing with Boolean objects?

<         


        
4条回答
  •  天涯浪人
    2021-02-08 09:40

    if (Boolean.TRUE == new Boolean(true)) {
        System.out.println("==");
    }
    
    if (Boolean.TRUE.equals(myBoolean)) {
        System.out.println("equals");
    }
    

    In this case first one is false. Only second if condition is true.

    It Prints:

    equals

提交回复
热议问题