What is the difference between & and && in Java?

前端 未结 13 2471
感情败类
感情败类 2020-11-22 10:33

I always thought that && operator in Java is used for verifying whether both its boolean operands are true, and the & oper

13条回答
  •  逝去的感伤
    2020-11-22 11:07

    && is a short circuit operator whereas & is a AND operator.

    Try this.

        String s = null;
        boolean b = false & s.isEmpty(); // NullPointerException
        boolean sb = false && s.isEmpty(); // sb is false
    

提交回复
热议问题