[Java] Operators

게맛살맛게·2021년 12월 2일
0

Java

목록 보기
4/18

Operator

용어

  • 연산 (operation) : 데이터를 처리하여 결과를 산출
  • 연산자 (operator) : 연산에 사용되는 표시나 기호 (데이터 처리 기능 수행)
  • 피연산자 (Operand) : 연산 되는 데이터
  • 연산식 (Expression) : 연산자와 피연산자를 이요, 연산 과정을 기술

연산자 우선순위

우선순위

1항

  • 후위 증감 var++ or var--
  • 전위 증감 ++var or --var
  • NOT !

2항

산술연산

  • 곱셈 *
  • 나눗셈 /
  • 나머지 %
  • 덧셈 +
  • 뺄셈 -

비트시프트연산

  • << >> >>>

비교연산 (관계연산)
- boolean 형의 결과 값을 얻음

  • 대소비교 < > <= >=
  • 일치여부 == !=

논리연산

  • 비트연산 & ^ |
  • AND 연산&&
  • OR 연산 ||

3항 condition ? trueCase : falseCase

할당(대입)연산자

대입연산자 =

  • = : 왼쪽의 변수에 오른쪽의 수식의 값을 대입한다
    - L-value에 R-value를 대입
    - L-value는 반드시 변수, R-value는 어떠한 수식이라도 가능

산술연산자

+ - * / %

  • 사칙연산에 사용되는 연산자
    % : 나누 나머지값을 반환

  • 예제

		int a = 7;
		int b = 3;
		
		// 부호 연산자(+, -)
		System.out.println(-a);			// -1 * a
		
		// 산술 연산자(+, -, *, /)
		System.out.println(a / b);		// int / int -> int(나눗셈의 몫)
		System.out.println(a % b);		// 정수 나눗셈의 나머지
		
//		System.out.println(a / 0);		// int / 0 -> Error
		System.out.println(7.0 / 0);		// Infinity
		System.out.println(7.0 / 0 + 100); 	// Infinity가 포함된 산술식 -> Infinity
		
		// Infinity Check
		System.out.println(Double.isInfinite(7.0 / 0));
		
		// Data가 NaN인지 확인
		System.out.println(0.0 / 0.0);
		System.out.println(Double.isNaN(0.0 / 0.0));
		System.out.println(Double.isNaN(7.0 / 0.0));

전위,후위연산자 ++, --

  • 전위연산자 ++x, --x
    - 실행문에서 값이 먼저 증가/감소 적용
    - 연산시 x값을 1 증가/감소 시킨 후 연산 수행
	int a = 7;
	int b = 0;

	b = ++a;
	System.out.println("b:" + b);	// b = 8
	System.out.println("a:" + a);	// a = 8
  • 후위연산자 x++, x--
    - 실행문이 끝난 후 증가/감소 적용
    - 연산시 연산이 끝난 후 x값을 1증가/감소
	int a = 7;
	int b = 0;

	b = a++;
	System.out.println("b:" + b);	// b = 7
	System.out.println("a:" + a);	// a = 8

비교연산 (관계연산)

  • 대소비교 < > <= >=
  • 일치여부 == : 일치 여부 (true/false 리턴) != 일치하지 않음 비교 (true/false 리턴)

논리연산자

리턴타입이 Boolean형 값을 결과로 하는 조건식만을 허용

연산자연산기호사용 예의미
AND 연산&&x && yx와 y가 모두 True이면 True, 하나라도 False일 경우 False
OR 연산||x || yx와 y가 하나라도 True일 경우 True, 모두 False일 때 False
NOT 연산!!xx의 값을 반전

비트연산자

  • & (and)
  • | (or)
  • ^ (xor)
  • ~ (not)
  • <<
  • >>
  • >>>
  • 예제
	// int에서만
	// 비트 단위의 미세한 제어에 이용
	int b1 = 0b11011101;
	int mask = 0b10101010;
	System.out.println(Integer.toBinaryString(b1));
	System.out.println(Integer.toBinaryString(mask));
	
	System.out.println(Integer.toBinaryString(b1 & mask));
	System.out.println(Integer.toBinaryString(b1 | mask));
	System.out.println(Integer.toBinaryString(~b1));
    
	int val = 1;

	System.out.println(Integer.toBinaryString(val));
	System.out.println(Integer.toBinaryString(val << 3));
	System.out.println(val << 3);
		
	val = 0b1000;
	System.out.println(Integer.toBinaryString(val));
	System.out.println(Integer.toBinaryString(val >> 2));
    
  • 실행 결과

3항 연산자

: 조건식에 따라 true면 : 앞쪽의 연산식을, false면 : 뒤쪽의 피연산자를 선택
condition ? trueCase : falseCase

  • 예제
	int a = 10;
		
	// a가 짝수면 짝수, 홀수면 홀수 출력
	System.out.println(a + "는 " + (a % 2 == 0 ? "짝수" : "홀수"));
		
	int score = 85;
	String message;
	// 점수가 90점 이상이면 Good, 50점 미만이면 Fail, 아니면 Pass
	System.out.println(message = score >= 90 ? "Good" : (score < 50 ? "Fail" : "Pass"));
	System.out.println(message);
  • 예제 결과
profile
IT 기술블로그

0개의 댓글