Operator 연산자

박성현·2024년 5월 3일

java

목록 보기
45/51

기본 연산

산술 연산자 : - +,-,*,%

package operatorex;

public class ArithmethicEx {

	public static void main(String[] args) {
		int x1 = 100;
		int x2 = 30;
		
		int add = x1 + x2;
		int sub = x1 - x2;
		int mul = x1 * x2;
		int mod = x1 % x2;
		System.out.println(add);
		System.out.println(sub);
		System.out.println(mul);
		System.out.println(mod);
		System.out.println("===========================");
		
		int iDiv = x1 / x2;
		
//		double(8bit)가 int(4bit)보다 크기때문에 자동형변환 됨
		double dDiv = x1 / x2;
		
		double dDiv2 = (double)x1/x2;
		
		System.out.println(iDiv);
		System.out.println(dDiv);
		System.out.println(dDiv2);
	}

}

결과값 :


증감연산자 ++, --

  • b = ++a : a 의 값을 증가시키고 증가된 값을 b에 넣어준다.
  • b = a++ : a 의 값을 먼저 b에 넣어준 후 a의 값을 증가시켜준다.
package operatorex;

public class ArithmethicEx {

	public static void main(String[] args) {
	int n = 0;
	int i = 0;
	
	n= ++i;
	System.out.printf("i=%s,n=%s %n", i, n);
	
	n= i++;
	System.out.printf("i=%s,n=%s %n", i, n);
	
	n= i--;
	System.out.printf("i=%s,n=%s %n", i, n);
	
	n= --i;
	System.out.printf("i=%s,n=%s %n", i, n);
	}
}

결과값 :


비교 연산자 : >, >=, <, <=, ==, !=

package operatorex;

public class ArithmethicEx {

	public static void main(String[] args) {
		int x1 = 100;
        int x2 = 10;

        boolean result = false;

        // == 동등 비교
        result = x1 == x2;
        System.out.println(result);

        // != 
        result = (x1 != x2);
        System.out.println(result);

        // >
        result = (x1 > x2);
        System.out.println(result);

        // >= 
        result = (x1 >= x2);
        System.out.println(result);

        // <
        result = (x1 < x2);
        System.out.println(result);

        // <= 
        result = (x1 <= x2);
        System.out.println(result);
	}
}

결과값 :


논리 연산자 : && , ||, !, ^


할당(대입) 연산자 : = , +=, -=, *=, /=, %=

package operatorex;

public class ArithmethicEx {

	public static void main(String[] args) {
		int i = 0;
		i = i + 1;
		System.out.println(i); // 1

		// +=
		i = i + 1;
		i += 1;
		System.out.println(i); // 3

		// -=
		i = i - 1;
		i -= 1;
		System.out.println(i); // 1

		// *=
		i = i * 9;
		i *= 9;
		System.out.println(i); // 81

		// /=
		i = i / 3;
		i /= 3;
		System.out.println(i); // 9

		// %=
		i = i % 3;
		i %= 3;
		System.out.println(i); // 0

	}
}

결과값 :


비트연산자 : & , |, ~, <<, >>, ^

package operatorex;

public class ArithmethicEx {

	public static void main(String[] args) {
		int x1 = 7;
		int x2 = 7;

		// 7 & 7 == 111 & 111 == 7 
		int result = x1 & x2;
		System.out.println(result);

		// 7 & 6 = 111 & 110 == 6
		x2 = 6;
		result = x1 & x2;
		System.out.println(result);

		// 7 & 1 = 111 & 001 == 1
		x2 = 1;
		result = x1 & x2;
		System.out.println(result);

		// 7 | 6 = 111 & 110 == 7
		x2 = 6;
		result = x1 | x2;
		System.out.println(result);

		// 7 | 1 = 111 & 001 == 7
		x2 = 1;
		result = x1 & x2;
		System.out.println(result);

	}
}

결과값 :


삼항연산 :(true|fauls) ? true일때 : false일때

  • iResult = (x1 > x2) ? (x1 - x2) : (x2 - x1) 를 해석해보면 x1 > x2가 참일 때 (x1 - x2)를 실행하고 거짓일 때 (x2 - x1)를 실행한다.
int x1 = 100;
        int x2 = 10;

        int iResult = 0;        
        String sResult = "";

        iResult = (x1 > x2) ? (x1 - x2) : (x2 - x1);        
        sResult = (x1 > x2) ? "x1이 크다." : "x2가 크다.";

        System.out.println(iResult);
        System.out.println(sResult);

결과값 :

profile
개발기록장

0개의 댓글