if statement checks for null but still throws a NullPointerException

后端 未结 6 2097
無奈伤痛
無奈伤痛 2020-12-05 09:35

In this code.

public class Test {
     public static void testFun(String str) {
         if (str == null | str.length() == 0) {
             System.out.print         


        
6条回答
  •  天涯浪人
    2020-12-05 10:16

    The | and & check both the sides everytime.

    if (str == null | str.length() == 0)

    here we have high possibility to get NullPointerException

    Logical || and && check the right hand side only if necessary.

    but with logical operator

    no chance to get NPE because it will not check RHS

提交回复
热议问题