오른쪽의 결과를 왼쪽에 대입(할당)한다 =
오른쪽과 왼쪽이 같을 때 == 로 표현
피연산자를 이용하여 +, -, *, /, % 등을 수행
package basicGrammar;
public class Variable {
public static void main(String[] args) {
int x = 1;
int y = 2;
// + 덧셈
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));
}
}
출력
x + y = 3
x - y = -1
x * y = 2
x / y = 0 // int형끼리의 연산으로 결과도 int로 표현
x % y = 1
산술 연산자와 대입 연산자를 결합한 연산자
package basicGrammar;
public class Variable {
public static void main(String[] args) {
int x = 100;
// + 덧셈
System.out.println("x += 2 : " + (x += 2));
// - 뺄셈
x = 100;
System.out.println("x -= 2 : " + (x -= 2));
// * 곱셈
x = 100;
System.out.println("x *= 2 : " + (x *= 2));
// / 나눗셈
x = 100;
System.out.println("x /= 2 : " + (x /= 2));
// % 나머지
x = 100;
System.out.println("x %= 2 : " + (x %= 2));
}
}
출력
x += 2 : 102
x -= 2 : 98
x *= 2 : 200
x /= 2 : 50
x %= 2 : 0
두개의 피연산자를 비교하여 참/거짓의 결론을 돌출
package basicGrammar;
public class Variable {
public static void main(String[] args) {
int x = 10;
int y = 20;
// a > b : a가 b보다 크면 참
System.out.println("x > y :" + (x > y));
// a < b : a가 b보다 작으면 참
System.out.println("x < y :" + (x < y));
// a >= b : a가 b보다 크거나 같으면 참
System.out.println("x >= y :" + (x >= y));
// a <= b : a가 b보다 작거나 같으면 참
System.out.println("x <= y :" + (x <= y));
// a == b : a가 b보다 같으면 참
System.out.println("x == y :" + (x == y));
// a != b : a가 b보다 같지 않으면 참
System.out.println("x != y :" + (x != y));
}
}
출력
x > y :false
x < y :true
x >= y :false
x <= y :true
x == y :false
x != y :true
1만큼 증가 하거나 감소를 수행
package basicGrammar;
public class Variable {
public static void main(String[] args) {
int x = 10;
// ++a : 1만크 증가 후 출력
System.out.println("++x : " + (++x));
// --a : 1만큼 감소 후 출력
x = 10;
System.out.println("--x : " + (--x));
// a++ : 출력 후 1만큼 증가
x = 10;
System.out.println("x++ : " + (x++));
System.out.println("x : " + x);
// a-- : 출력 후 1만큼 감소
x = 10;
System.out.println("--x : " + (x--));
System.out.println("x : " + x);
}
}
출력
++x : 11
--x : 9
x++ : 10
x : 11
--x : 10
x : 9
피연산자의 논리곱(AND), 논리합(OR), 논리부정(NOT) 수행
package basicGrammar;
public class Variable {
public static void main(String[] args) {
boolean b1 = false;
boolean b2 = true;
// && 논리곱(AND) : 모두 참이면 참
System.out.println("b1 && b2 : " + (b1 && b2));
// || 논리합(OR) : 하나라도 참이면 참
System.out.println("b1 || b2 : " + (b1 || b2));
// ! 논리부정(NOT) 상태 부정
System.out.println("!b1 = " + (!b1));
System.out.println("!b2 = " + (!b2));
}
}
출력
b1 && b2 : false
b1 || b2 : true
!b1 = true
!b2 = false
삼항 연산자로 두개의 피연산자 연산 결과에 따라 나머지 피연산자가 결정
package basicGrammar;
public class Variable {
public static void main(String[] args) {
int x = 10;
int 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
데이터를 bit단위로 환산하여 연산을 수행
다른 연산자보다 연산 속도가 향상
package basicGrammar;
public class Variable {
public static void main(String[] args) {
int x = 2; // bit : 00000010
int y = 3; // bit : 00000011
// & : AND 연산, 모두 1이면 1
System.out.println("x & y : " + (x & y));
// | : OR 연산, 하나라도 1이면 1
System.out.println("x | y : " + (x | y));
// ^ : XOR 연산, 서로 같지 않으면 1
System.out.println("x ^ y : " + (x ^ y));
}
}
출력
x & y : 2 // 00000010
x | y : 3 // 00000011
x ^ y : 1 // 00000001