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

JAVA(자바) - 단항 연산자

by You진 2020. 12. 18.

단항 연산자

- 피연산자가 1개인 연산자

- 종류

  1. 부호 연산자 : +, -

  2. 증감 연산자 : ++, --

  3. 부정 연산자 : !

  4. 비트 반전 연산자 : ~

 

부호 연산자 : +, -

연산식 설명
+ 피연산자 피연산자의 부호 유지
- 피연산자 피연산자의 부호 변경

-boolean 타입과 char 타입을 제외한 기본 타입에 사용 가능

public class SingOperationExample {
	public static void main(String[] args) {
		int x = 100;
		int result1 = +x;
		int result2 = -x;
		System.out.println("result1 ="+result1);
		System.out.println("result2 ="+result2);
		
		short s = 100;
		//short result3 = -s; 컴파일 오류 이유 : 부호 연산자 산출은 int 타입 된다.
		//short 타입 값을 부호 연산하면 int 타입 값으로 바뀐다.(자동으로 타입변환이 이뤄진다)
		
		int result3 = -s;
		System.out.println("result3 ="+result3);
	}
}
//result1 =100
//result2 =-100
//result3 =-100

 

증감 연산자 : ++, --

연산식  
++ 피연산자 다른 연산을 수행하기 전에 피연산자의 값을 1 증가 시킴
-- 피연산자 다른 연산을 수행하기 전에 피연산자의 값을 1 감소 시킴
피연산자 ++ 다른 연산을 수행한 후에 피연산자의 값을 1 증가 시킴
피연산자 -- 다른 연산을 수행한 후에 피연산자의 값을 1 감소 시킴
public class IncreaseDecreaseOperatorExample {
	public static void main(String[] args) {
		int x = 10;
		int y = 10;
		int z;
		
		System.out.println("---------------------");
		x++; 
		++x; 
		System.out.println("x="+x);
		System.out.println("---------------------");
		y--; 
		--y; 
		System.out.println("y="+y);
		System.out.println("---------------------");
		z=x++; 
		System.out.println("z="+z);
		System.out.println("x="+x);
		System.out.println("---------------------");
		z=++x;
		System.out.println("z="+z);
		System.out.println("x="+x);
		System.out.println("---------------------");
		z = ++x + y++; 
		System.out.println("z="+z);
		System.out.println("x="+x);
		System.out.println("y="+y);
	}
}
---------------------
x=12
---------------------
y=8
---------------------
z=12
x=13
---------------------
z=14
x=14
---------------------
z=23
x=15
y=9

 

논리 부정 연산자 : !

연산식 설명
! 피연산자 피연산자가 true 이면 false 값을 산출
피연산자가 false 이면 true 값을 산출

- boolean 타입만 피연산자가 될 수 있다.

public class DenyLogicOperatorExample {
	public static void main(String[] args) {
		boolean play = true;
		System.out.println(play);
		
		play = !play;
		System.out.println(play);
		
		play = !play;
		System.out.println(play);
	}
}

//true
//false
//true

 

비트 반전 연산자: ~

연산식 설명
~ 10 (0 0 ... 0 1 0 1 0) 산출결과: -11 (1 1 ... 1 0 1 0 1)

-byte, short, int, long 타입만 피연산자가 될 수 있다.

-비트값을 반전(0->1, 1->0) 시킨다.

-부호 비트인 최상위 비트까지 반전되므로 부호가 반대인 새로운 값이 산출된다.

-피연산자의타입이 int 이하이면 연산의 결과는 int 타입이다.

-비트 반전후 1을 더하면 부호가 반대인 값을 얻을 수 있다.

 

public class BitReverseOperatorExample {
	public static void main(String[] args) {
		int v1 = 10;
		int v2 = ~v1;
		int v3 = ~v1 +1;
		System.out.println(toBinaryString(v1)+"(십진수: "+v1+")");
		System.out.println(toBinaryString(v2)+"(십진수: "+v2+")");
		System.out.println(toBinaryString(v3)+"(십진수: "+v3+")");
		System.out.println();
		int v4 = -10;
		int v5 = ~v4;
		int v6 = ~v4+1;
		System.out.println(toBinaryString(v4)+"(십진수: "+v4+")");
		System.out.println(toBinaryString(v5)+"(십진수: "+v5+")");
		System.out.println(toBinaryString(v6)+"(십진수: "+v6+")");
	}
	public static String toBinaryString(int value) {
		String str = Integer.toBinaryString(value);
		while(str.length()<32) {
			str ="0"+str;
		}
		return str;
	}
}