본문 바로가기

개발일지/Java + Spring

[java] 연산자 모음

연산자의 우선순위

종류 종류 우선순위
최우선 연산자 ., [], () 아래로 갈수록 우선순위가 낮고, 우선순위가 같으면 좌측부터 연산한다.
단항 연산자 ++, --, !, ~, +/-
산술 연산자 +, -, *, /, %
쉬프트 연산자 >>, <<, >>>
비교 연산자 >, <, >=, <=, ==, !=
비트 연산자 &, |, ^, ~
논리 연산자 &&, ||, !
삼항 연산자 (조건식) ? :
대입 연산자 =, *=, /=, %=, +=, -=

산술 연산자

public class OperationTest1 {
	/*
	산술 연산자 테스트
	*/
	public static void main(String[] args) {

		int result = 0;
		int var_int1 = 10;
		int var_int2 = 2;
		
		// + 연산
		result = var_int1 + var_int2;
		System.out.println("var_int1 + var_int2 = " + result);
		
		// - 연산
		result = var_int1 - var_int2;
		System.out.println("var_int1 - var_int2 = " + result);
		
		// * 연산
		result = var_int1 * var_int2;
		System.out.println("var_int1 * var_int2 = " + result);
		
		// / 연산
		result = var_int1 / var_int2;
		System.out.println("var_int1 / var_int2 = " + result);
		
		// % 연산
		result = var_int1 % var_int2;
		System.out.println("var_int % var_int2 = " + result);
	}

}


비교 연산자

두 개의 값을 비교해서 boolean 타입으로 반환한다.
>, <, >=, <=, !=
참이면 true, 거짓이면 false

 


논리 연산자

 

boolean 타입의 데이터를 연산해서 결과값으로 boolean 타입의 데이터를 반환한다. 

 

&& (AND 연산자)
a == 7 && b == 3
a가 7이고 b가 3이다. 
둘 다 만족해야 true

|| (OR 연산자)
a == 7 || b == 3
a가 7이거나, b가 3이라면..
둘 중에 하나만 만족해도 true


증감 연산자

public class OperationTest5 {

	public static void main(String[] args) {
		int var_inc = 1;
		int var_dec = 1;
		int result = 0;
		
		// ++
		result = var_inc++;
		//1을 먼저 보내놓고 +1이 돼서 1이 출력
		System.out.println("result = " + result);
		System.out.println("var_inc = " + var_inc);
		
		System.out.println("-------------------");
		
		result = ++var_inc;
		//+1을 하고 나서 보내서 3이 출력
		System.out.println("result = " + result);
		System.out.println("var_inc = " + var_inc);
		
		System.out.println("-------------------");
		
		// --
		result = var_dec--;
		System.out.println("result = " + result);
		System.out.println("result = " + var_dec);
		
		System.out.println("-------------------");
		
		result = --var_dec;
		System.out.println("result = " + result);
		System.out.println("result = " + var_dec);

	}

}

 

변수 값을 1씩 증가(++) 하거나, 감소(--)시킨다. 변수의 앞, 뒤 위치에 따라서 다른 연산을 실행한다. 변수의 앞에 오는 경우 연산자를 먼저 실행하고 다른 연산을 실행한다. 변수의 뒤에 오는 경우 다른 연산을 먼저 실행하고 나중에 실행을 한다.

 

 


10진수 -> 2진수 / 2진수 -> 10진수

10진수 16을 2진수로 바꾸려면 16%2를 해주면 된다. 끝까지 계산 했을 때 나머지를 계산하면 나오는 숫자가 바로 2진수다.(10000) 다시 2진수를 10진수로 바꾸려면 아주 쉬운 방법이 있다. 2진수 값 아래에 32 16 8 4 2 1 이런식으로 오른쪽부터 곱하기 2씩 써두고, 1인 부분만 서로 더하면 끝이다. 


비트 연산자

public class OperationTest6 {

	public static void main(String[] args) {
		int x = 8;
		int y = 3;
		int result = 0;
		boolean bResult = false;
		
		/*
		 * 비트로 변경 
		 * 8 : 00000000 00000000 00000000 00001000 
		 * 3 : 00000000 00000000 00000000 00000011
		 * 8개 라는 건 int가
		 *  4바이트 라는 뜻.. 이건 8바이트
		 */
		
		// &
		result = x & y;
		System.out.println("x & y = " + result);
		bResult = true & false;
		System.out.println("true & false = " + bResult);
		
		// |
		result = x | y;
		System.out.println("x | y = " + result);
		bResult = true | false;
		System.out.println("true | false = " + bResult);
		
		// ^
		result = x ^ y;
		System.out.println(" x ^ y = " + result);
		bResult = true ^ false;
		System.out.println("true ^ false = " + bResult);

	}

}

 

값을 비트로 변경해서 연산한다.

& : 모두 1일 때 1을 반환
| : 값 중 하나라도 1이면 1을 반환
^ : 두 값이 모두 같으면 0, 값이 틀리면 1

 


쉬프트 연산자

public class OperrationTest7 {
	public static void main(String[] args) {
		int x = 3;
		int result = 0;
		
		// <<
		result = x << 2;
		System.out.println("x << 2 = " + result);
		
		// >> 
		x = -1;
		result = x >> 1;
		System.out.println("x >> 1 = " + result);
		
		// >>>
		result = x >>> 1;
		System.out.println("x >>> 1 = " + result);
	}
}

값을 비트로 바꾼 뒤 왼쪽 오른쪽 지정한 만큼 움직인다. 꼭지가 어딜 향해있는지 보면 됨

>> 오른쪽으로 비트 이동. 맨 왼쪽 비트가 1이면 오른쪽 비트로 이동 했을 때 1로 채움

>>> 오른쪽으로 비트 이동. 좌측에 남는 비트 영역을 무조건 0으로 채움

<< 왼쪽으로 비트 이동. 오른쪽에 비는 비트 영역을 0으로 채움

 

비트로 바꾼다는 것은 2진수로 바꾼다는 뜻

 

<< 왼쪽으로 민 것은 (x*2)

0 0 1 1 0 0
0 1 1 0 0 0

위 값은 : 12

아래 값은 : 24

 

>> 오른쪽으로 민 것은 (x/2)

0 0 1 1 0 0
0 0 0 1 1 0

위 값 : 12

아래 값 : 6


복합 연산자

/*
 * 복합 연산자
 * 연산 후 대입 연산자
*/

public class OperationTest8 {

	public static void main(String[] args) {
		int x = 5;
		
		// +=
		x += 5; //연산을 먼저 하고 대입을 한다.
		System.out.println("x += 5 = " + x);
		x += 3.1; //정수형이라서 0.1은 잘린다.
		System.out.println("x += 3.1 = " + x);
		
		// *=
		x *= 2; 
		System.out.println("x *= 2 = " + x);
		
		// /=
		x /= 2;
		System.out.println("x /= 2 = " + x);
		
		// -=
		x -= 5;
		System.out.println("x -= 5 = " + x);
		
		// %=
		x %= 2;
		System.out.println("x %= 2 = " + x);

	}

}

연산자를 먼저 계산하고 그 값을 변수에 대입 

+=, -=, *=, /=, %=


삼항 연산자

public class OperationTest9 {

	public static void main(String[] args) {
		int num = Integer.parseInt(args[0]);
		String msg = (num % 2 == 0) ? "짝수" : "홀수";
		System.out.println(num + "은 " + msg + "입니다.");

	}

}

(조건식) ? 참일 때 : 거짓일 때