BackEnd/JAVA

JAVA 연산자 (증감, 산술, 시프트, 비교, 비트, 논리, 조건, 대입)

H_Develop 2022. 7. 20. 16:06

최고 연산자     ' . '  () Integer.parseInt(), 상속(inherit)에서도 부모를 parent.child 식으로 표시
증감 연산자     ++, --
산술 연산자     + - * / %(moduler:나머지)
시프트 연산자(비트 이동 연산자)     >>  <<  
                                                        0011 <<2 : 1100 이동 후, 뒤에 채워지는 00을 padding이라 한다.
                                                        맨 첫번째 비트가 1이면 ( - ) 부호를 의미한다. 결과적으로 1100 은 -4가 된다.
비교 연산자      >(more than)  <(less than)  >=(more equal)  <=(less equal)  ==(equal)  !=(not equal)
                         if while .. 등에서 조건을 표시할 때 주로 사용된다.
비트 연산자     &(and)  |(or)  ^(xor:exclusibe(배타적) or)  ~(not)
                        1100 & 1010 :      값  1000 (둘다 1일 때, 계산된다.)
                        1100  |  1010 :      값  1110 (하나가 1일 때, 계산된다.)
                        1100 ^  1010 : 1100 | 0101 값 1101
논리 연산자     &&(and)  || (or)  ! (not)
                        if while .. 등에서 조건을 표시할 때 주로 사용된다.
조건 연산자    (삼항 연산자) 조건 ? 참 : 거짓
대입 연산자    =(우측 값을 좌측에 대입) +=  -=  *=  /=  %=
                       a *= 3  >  a = a * 3 계산이 빠르기 때문에 사용한다.

 

 

기본 연산자

 

public class java01 {
	public static void main(String[] args) {
		
		int n1=10, n2=7;
		System.out.println("n1 : " + n1 + " n2 : " + n2);
		
		n3 = 13; int n4 = 15;
		System.out.println("n3 + n4 : " + (n3 += n4) );
		
		int n5 =10; int n6 = 3;
		System.out.println("n5 / n6 : " + (n5 /= n6));

		int n7 = 10, n8 = 3;
		int n9 = n7 %= n8;
		System.out.println(n9);
	}
}

n1 : 10 n2 : 7
n3 + n4 : 28
n5 / n6 : 3
1

 

비교 연산자

 

public class java01 {
	public static void main(String[] args) {
		
		int n1 = 10; int n2 = 20;
		boolean result;
		result = n1 < n2;
		// boolean result = n1 < n2;  
		System.out.println(result);
		
		result = n1 == n2;
		System.out.println(result);

		result = n1 != n2;
		System.out.println(result);
	}
}

true
false
true

 

논리 연산자

 

public class java01 {
	public static void main(String[] args) {

		int myAge = 30;
		int limit = 35;
		boolean result = ((limit - myAge) >= 5 && myAge > 30);
		
		System.out.println(result);
		System.out.println();
		
		int n1 = 10, n2=20;
		boolean result2 = (n1 += 10) > 20 || (n2-10 == 11);
		
		System.out.println(result2);
		System.out.println(!result2);
	}
}

false

false
true

 

 

비트 연산자 (2진수에서만 작용)

 

public class java01 {
	public static void main(String[] args){
		int a = 10; // 1010
        int b = 7;  // 0111
        int c = a&b; // 논리 곱 : 2진수로 모두 1일때만 1, 나머지는 0
		System.out.println(c);
        
        int a2 = 12;
        int b2 = 8;
        int c2 = a2 | b2; // 논리 합 : 두 값 모두 0일때만 0, 나머지는 1
    	System.out.println(c2);
        
        int a3 = 9;
        int b3 = 11;
        int c3 = a3 ^ b3; // 배타적 or(XOR) : 두 값이 서로 같을 때만 0, 서로 다를 때는 1
        System.out.println(c3);
    }
}

2
12
2

 

비트 이동 연산자

 

public class java01 {
	public static void main(String[] args) {
		int a = 12; // 1100
		int b = 2; // 0010
		int c = a >> b; // c = 1100 >> 2 == 0011
		System.out.println(c);
		
		int d = c << b; // 0011 << 2 == 1100
		System.out.println(d);
		
		char ch = 'F'; // ASCII F == 70, 01000110
		int num = 1;
		char cha_result = (char)(ch >> num); 
		// 00100011 == 35 == (ASCII CODE) #
		System.out.println(cha_result);
	}
}

3
12
#

 

 

증감 연산자

 

public class java01 {
	public static void main(String[] args) {
		int a = 10;
		System.out.println(++a); // 11, 연산 후 출력
		int b = 10;
		System.out.println(b++); // 10, 출력 후 연산
		System.out.println(b);	 // 11, 위에서 연산되어 출력
	}
}

11
10
11

 

 

3항 연산자

 

public class java01 {
	public static void main(String[] args) {
		int a = 10;
		int b = 15;
		boolean result = ++a >= b ? true : false;
		System.out.println(result);
		System.out.println();
		
		int n1 = 10;
		int n2 = 20;
		char result2;
		result2 = (n1 += n1) == n2 ? 'O' : 'X';
		System.out.println(result2);
	}
}

false

O

 

 

 

 

 


아이 상태 확인 후 탑승여부 결정

 

import java.util.Scanner;
public class practise {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int age, height; 
		String health;
		
		System.out.println("아이 나이를 입력해주세요");
		age = sc.nextInt();
		System.out.println("아이 키를 입력해주세요");
		height = sc.nextInt();
		System.out.println("아이의 상태를 입력해주세요 good/bad");
		health = sc.next();

		String result = (age >= 6 || height >= 130) && health.equals("good") ? "탑승 가능합니다." : "탑승이 불가합니다.";
				
		System.out.println(result);
	}
}

문자열 확인하기 위해, String 클래스는 equals() 함수를 사용해야 변수 값을 비교한다.
== 를 사용하면, 주소값 비교.