Develop/JAVA

JAVA 연산자

roalwh 2021. 10. 10. 17:57
package calculation;

public class Calculation {
	public static void main(String[] args) {
		
		int x = 10; int y = 100;
		
		System.out.println("대입연산자");
		System.out.println("x="+x);
		System.out.println("y="+y);
		System.out.println();
		
		x=y;
		System.out.println("x=y 대입");
		System.out.println("x="+x);
		System.out.println("y="+y);
	
		/* 결과값 
		*대입연산자
		*x=10
		*y=100

		*x=y 대입
		*x=100
		*y=100
		*/
		
		x = 10; y=20;
		System.out.println();
		System.out.println("산술연산자 "+"x="+x+", y="+y);
		System.out.println("x + y = " + (x + y));	//덧셈
		System.out.println("x - y = " + (x - y));	//뺄셈
		System.out.println("x * y = " + (x * y));	//곱셈
		System.out.println("x / y = " + (x / y));	//나눗셈
		System.out.println("x % y = " + (x % y));	//나머지

		System.out.println();
		System.out.println("복합 대입연산자"+" x="+x);
		x=10;
		System.out.println("x = +=10 : " + (x += x));	//더하고대입
		x=10;
		System.out.println("x = -=10 : " + (x -= x));	//뺴고 대입
		x=10;
		System.out.println("x = *=10 : " + (x *= 10));	//곱하고 대입
		x=10;
		System.out.println("x = /=10 : " + (x /= 10));	//나누고 대입
		x=10;
		System.out.println("x = %=10 : " + (x %= 10));	//나머지를 대입
		
		/*결과값
		 * x = +=10 : 20
		 * x = -=10 : 0
		 * x = *=10 : 100
		 * x = /=10 : 1
		 * x = %=10 : 0
		 */
		
		//관계연산자
		x = 10; y= 20;
		System.out.println();
		System.out.println("관계연산자");
		System.out.println("x > y : " + (x > y));	//x 가 y보다 크면 참
		System.out.println("x < y : " + (x < y));	//x 가 y보다 작으면 참
		System.out.println("x >= y : " + (x >= y));	//x 가 y보다 크거나 같으면 참
		System.out.println("x <= y : " + (x <= y));	//x 가 y보다 작거나 같으면 참
		System.out.println("x == y : " + (x == y));	//x 가 y보다 같으면 참
		System.out.println("x != y : " + (x != y));	//x 가 y보다 같지 않으면 참
		
		/*결과값
		 * x > y : false
		 * x < y : true
		 * x >= y : false
		 * x <= y : true
		 * x == y : false
		 * x != y : true
		 */
		
		//증감연산자
		
		System.out.println();
		System.out.println("증감연산자");
		x = 10;
		System.out.println("++x : " + (++x));	//증가 후 대입
		x = 10;
		System.out.println("--x : " + (--x));	//감소 후 대입
		x = 10;
		System.out.println("x++ : " + (x++));	//대입 후 증가
		System.out.println("x : " + x);
		x = 10;
		System.out.println("x-- : " + (x--));	//대입 후 감소
		System.out.println("x : " + x);
		
		/*결과창
		 * ++x : 11
		 * --x : 9
		 * x++ : 10
		 * x : 11
		 * x-- : 10
		 * x : 9
		 */
		
		//조건(삼항)연산자
		
		System.out.println();
		System.out.println("조건(삼항)연산자");
		x = 10; y = 20;
		int result = 0;
		result = ( x >  y) ? 100 : 200;
		System.out.println("result : " + result);
		
		result = ( x <  y) ? 100 : 200;
		System.out.println("result : " + result);
		
		result = ( x ==  y) ? 100 : 200;
		System.out.println("result : " + result);
		
		/*결과값
		 * result : 200
		 * result : 100
		 * result : 200
		 */
		
		//비트 연산자
		x = 0; y = 1;
		System.out.println();
		System.out.println("비트연산자");
		System.out.println("x & y : " + (x & y));		//AND 연산
		System.out.println("x | y : " + (x | y));		//OR 연산
		System.out.println("x ^ y : " + (x ^ y));		//XOR 연산
		
		/*결과값
		 * x & y : 0
		 * x | y : 1
		 * x ^ y : 1
		 */
		
		

	}

}

'Develop > JAVA' 카테고리의 다른 글

JAVA 배열과 메모리  (0) 2021.10.18
JAVA 배열, Scanner  (0) 2021.10.14
JAVA 특수문자와 서식문자  (0) 2021.10.04
JAVA 변수 선언 및 자료형  (0) 2021.09.26
JAVA 이클립스 설정  (0) 2021.09.26