본문 바로가기
자바공부/연산자

JAVA(자바) - 이항 연산자 6.대입 연산자(=, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>=)

by You진 2021. 1. 19.

6.JAVA(자바) - 이항 연산자 6.대입 연산자(=, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>=, >>>=)

- 오른쪽 피연산자의 값을 좌측 피연산자인 변수에 저장

- 종류

 ● 단순 대입 연산자

 ● 복합 대입 연산자

     (정해진 연산을 수행한 후 결과를 변수에 저장)

구분 연산자 설명
단순 대입 연산자 변수 = 피연산자 변수 = 연산자
복합 대입 연산자 변수 += 연산자 변수 = 변수 + 연산자
변수 -= 연산자 변수 = 변수 - 연산자
변수 *= 연산자 변수 = 변수 * 연산자
변수 /= 연산자 변수 = 변수 / 연산자
변수 %= 연산자 변수 = 변수 % 연산자
변수 &= 연산자 변수 = 변수 & 연산자
변수 |= 연산자 변수 = 변수 | 피연산자
변수 <<= 연산자 변수 = 변수 << 피연산자
변수 >>= 연산자 변수 = 변수 >> 피연산자
변수 >>>= 연산자 변수 = 변수 >>> 피연산자
	public static void main(String[] args) {
		int result = 10;
		
		result += 10;
		System.out.println("result += "+result);
		
		result -= 5;
		System.out.println("result -= "+result);
		
		result *= 3;
		System.out.println("result *= "+result);
		
		result /= 5;
		System.out.println("result /= "+result);
		
		result %= 5;
		System.out.println("result %= "+result);
		System.out.println();
		
		위 실행 값:
		result += 20
		result -= 15
		result *= 45
		result /= 9
		result %= 4
		
		
		int result1 = 45;
		
		result1 &= 25;
		System.out.println("result1 &= "+result1);
		
		result1 |= 5;
		System.out.println("result1 |= "+result1);		
		
		result1 <<= 3;
		System.out.println("result1 <<= "+result1);
		
		result1 >>= 3;
		System.out.println("result1 >>= "+result1);
		
		result1 >>>= 3;
		System.out.println("result1 >>>= "+result1);
        
		위 실행 값:
		result1 &= 9
		result1 |= 13
		result1 <<= 104
		result1 >>= 13
		result1 >>>= 1
	}