Notice
Recent Posts
Recent Comments
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Archives
Today
Total
관리 메뉴

충분히 쌓여가는

3.7 논리 연산자 본문

이것이 자바다/03 연산자

3.7 논리 연산자

빌드이너프 2024. 3. 29. 10:54

&&과 &의 산출 결과는 같지만 연산 과정이 조금 다른다

&&는 앞의 피연산자가 false라면 뒤의 피연산자를 평가하지 않고 바로 false를 산출

&는 두 피연산자 모두를 평가해서 산출 결과를 나타낸다

따라서 &보다 &&가 더 효율적으로 동작한다

 

||는 앞의 피연산자가 true라면 뒤의 피연산자를 평가하지 않고 바로 true를 산출

|는 두 피연산자 모두 평가해서 산출

 

package ch03.sec07;

public class LogicalOperatorExample {
	public static void main(String[] args) {
		int charCode = 'A';
		// int charCode = 'a';
		
		if( (65<=charCode) & (charCode<=90) ) {
			System.out.println("대문자");
		}
		
		if( (97<=charCode) && (charCode<=122) ) {
			System.out.println("소문자");
		}
		
		// ===========================================
		
		int value = 6;
		// int value = 7;
		
		if( (value%2==0) || (value%3==0) ) {
			System.out.println("2 또는 3의 배수");
		}
		
		boolean result = (value%2==0) || (value%3==0);
		
		if(!result) {
			System.out.println("2 또는 3의 배수가 아님");
		}
	}
}
대문자
2 또는 3의 배수