java

if 비교 연산자 우선순위

기계새 2014. 4. 1. 11:16

비교연산자는 앞문항의 값을 비교 후 다음문항을 검사한다.

str == null 검사가 뒤에 있을경우 equals 검사를 우선수행하게 되어 NullPointException이 발생하게 된다.


ex)

private void check(String str) {

  if(str == null || str.equals("check") { 

  //if("check".equals(str)) // 이렇게 써주는게 더 효과적이다. 

    throw new Exception();

  } else {

    return;

  }

}