()
++
--
!
~
new
(type)
*
/
%
-> +
-
<<
>>
>>>
<
<=
>
>=
instanceof
==
!=
&
^
ㅣ
&&
||
? :
=
+=
-=
*=
/=
%=
왠만하면 괄호를 이용하자
라는 것이고 이렇게 하면 연산자 우선 순위를 달달 외울 필요없이 사용하실 수 있으실 것이라 생각합니다.2 + 3 * 4 - 12 / 3
-> 2 + (3 * 4) - (12 / 3)
연산자 | 설명 | 예시 | 결과 |
---|---|---|---|
+ | 덧셈 | a + b | a와 b의 합 |
- | 뺄셈 | a - b | a에서 b를 뺌 |
* | 곱셈 | a * b | a와 b의 곱 |
/ | 나눗셈 | a / b | a를 b로 나눈 몫(정수), 결과(실수) |
% | 나머지 | a % b | a를 b로 나눈 나머지 |
++ | 증가 (1 증가) | a++ 또는 ++a | a를 1 증가시킴 |
-- | 감소 (1 감소) | a-- 또는 --a | a를 1 감소시킴 |
Arithmetic1.java
package operators;
public class Arithmetic1 {
public static void main(String[] args) {
// 1. 정수형 나눗셈
int a = 10;
int b = 3;
System.out.println("a / b = " + a / b); // 3 (몫)
// 2. 실수형 나눗셈
double c = 10.0;
double d = 3.0;
System.out.println("c / d = " + c / d); // 3.33333...
}
}
Arithmetic1.class
a / b = 3
c / d = 3.3333333333333335
++
와 --
는 증감연산자라 하여 값을 1씩 증가시키거나 감소시키는 명령어입니다. 그런데 사용 위치에따라 동작하는 방식의 차이가 있습니다.++a
; 전위 증감연산자)a++
; 후위 증감연산자)Arithmetic2.java
package operators;
public class Arithmetic2 {
public static void main(String[] args) {
// 1. 전위 증감연산자
int a = 10;
int b = 5;
System.out.println("a = " + a + ", b = " + b);
int result = ++a * b;
System.out.println("++a * b = " + result); // 55
System.out.println("a = " + a + ", b = " + b); // a = 11, b = 5
System.out.println("-----------------------");
// 2. 후위 증감연산자
int c = 10;
int d = 5;
System.out.println("c = " + c + ", d = " + d);
int result2 = c++ * d;
System.out.println("c++ * d = " + result2); // 50
System.out.println("c = " + c + ", d = " + d); // c = 11, d = 5
System.out.println("-----------------------");
}
}
Arithmetic2.class
a = 10, b = 5
++a * b = 55
a = 11, b = 5
-----------------------
c = 10, d = 5
c++ * d = 50
c = 11, d = 5
-----------------------
++
나 --
가 붙으면 변수를 먼저 바꾸고 연산을 수행한다.++
나 --
가 붙으면 연산을 먼저 수행하고 변수를 바꾼다.연산자 | 설명 | 예시 | 결과 |
---|---|---|---|
== | 같음 | a == b | a가 b와 같으면 true |
!= | 같지 않음 | a != b | a가 b와 다르면 true |
> | 큼 | a > b | a가 b보다 크면 true |
< | 작음 | a < b | a가 b보다 작으면 true |
>= | 크거나 같음 | a >= b | a가 b보다 크거나 같으면 true |
<= | 작거나 같음 | a <= b | a가 b보다 작거나 같으면 true |
==
과 !=
에서 숫자형은 문제가 없으나, 문자열을 비교하는 경우에는 "문자열1".equals("문자열2")
의 형태로 비교해야한다는 것입니다.==
과 !=
의 경우 값을 비교하는 것이 아니라 메모리의 참조값을 비교하기때문에 나타나는 현상인데, 자세한건 지금 알려고 하실 필요는 없습니다.Relational.java
package operators;
public class Relational {
public static void main(String[] args) {
// 1. 비교 연산자
int a = 10;
int b = 20;
int c = 10;
System.out.println("a == b : " + (a == b)); // false
System.out.println("a == c : " + (a == c)); // true
System.out.println("a != b : " + (a != b)); // true
System.out.println("a != c : " + (a != c)); // false
System.out.println("a > b : " + (a > b)); // false
System.out.println("a < b : " + (a < b)); // true
System.out.println("a >= b : " + (a >= b)); // false
System.out.println("a <= b : " + (a <= b)); // true
System.out.println("-----------------------");
// 2. 문자열 비교
String str1 = "hello";
String str2 = "hello";
String str3 = new String("hello");
System.out.println("str1 == str2 : " + (str1 == str2)); // true
System.out.println("str1 == str3 : " + (str1 == str3)); // false
System.out.println("str1.equals(str3) : " + str1.equals(str3)); // true
}
}
Relational.class
a == b : false
a == c : true
a != b : true
a != c : false
a > b : false
a < b : true
a >= b : false
a <= b : true
-----------------------
str1 == str2 : true
str1 == str3 : false
str1.equals(str3) : true
연산자 | 설명 | 예시 | 결과 |
---|---|---|---|
&& | 논리 AND | a && b | a와 b가 모두 참이면 true |
|| | 논리 OR | a || b | a혹은 b가 참이면 true |
! | 논리 NOT | !a | a가 참이면 false, 거짓이면 true |
()
를 붙여서 해주는 것이 가독성과 우선순위 설정에 좋습니다.Logical.java
package operators;
public class Logical {
public static void main(String[] args) {
// 1. 논리 연산자
boolean a = true;
boolean b = false;
System.out.println("a && b : " + (a && b)); // false
System.out.println("a || b : " + (a || b)); // true
System.out.println("!a : " + !a); // false
System.out.println("!b : " + !b); // true
System.out.println("-----------------------");
// 2. 논리 연산자와 비교 연산자
int c = 10;
int d = -10;
System.out.println("(c > 0) && (d > 0) : " + ((c > 0) && (d > 0))); // false
System.out.println("(c > 0) || (d > 0) : " + ((c > 0) || (d > 0))); // true
}
}
Logical.class
a && b : false
a || b : true
!a : false
!b : true
-----------------------
(c > 0) && (d > 0) : false
(c > 0) || (d > 0) : true
연산자 | 설명 | 예시 | 결과 |
---|---|---|---|
& | 비트 AND | a & b | a와 b의 비트가 모두 1이면 1 |
| | 비트 OR | a | b | a와 b의 비트가 하나라도 1이면 1 |
^ | 비트 XOR | a ^ b | a와 b의 비트가 서로 다르면 1 |
~ | 비트 NOT | ~a | a의 비트를 반전 |
<< | 왼쪽 시프트 | a << 2 | a의 비트를 2만큼 왼쪽으로 시프트 |
>> | 오른쪽 시프트 | a >> 2 | a의 비트를 2만큼 오른쪽으로 시프트 |
>>> | 오른쪽 시프트 (부호 없음) | a >>> 2 | a의 비트를 2만큼 오른쪽으로 시프트(부호 없음) |
Bitwise.java
package operators;
public class Bitwise {
public static void main(String[] args) {
// 1. 비트 논리 연산자
int a = 200; // 11001000
int b = 127; // 01111111
System.out.println("a & b : " + (a & b) + "(" + Integer.toBinaryString(a & b) + ")"); // 72(1001000)
System.out.println("a | b : " + (a | b) + "(" + Integer.toBinaryString(a | b) + ")"); // 255(11111111)
System.out.println("a ^ b : " + (a ^ b) + "(" + Integer.toBinaryString(a ^ b) + ")"); // 183(10110111)
System.out.println("-----------------------");
// 2. 비트 반전 & 이동 연산자
int c = -77; // 10110011
System.out.println("~c : " + (~c) + "(" + Integer.toBinaryString(~c) + ")"); // 76(1001100)
System.out.println("c << 2 : " + (c << 2) + "(" + Integer.toBinaryString(c << 2) + ")"); // -308(11001100)
System.out.println("c >> 2 : " + (c >> 2) + "(" + Integer.toBinaryString(c >> 2) + ")"); // -20(11101100)
System.out.println("c >>> 2 : " + (c >>> 2) + "(" + Integer.toBinaryString(c >>> 2) + ")"); // 1073741804(11111111111111111111111111001100)
}
}
Bitwise.class
a & b : 72(1001000)
a | b : 255(11111111)
a ^ b : 183(10110111)
-----------------------
~c : 76(1001100)
c << 2 : -308(11111111111111111111111011001100)
c >> 2 : -20(11111111111111111111111111101100)
c >>> 2 : 1073741804(111111111111111111111111101100)
&
연산자가 쓰이는 예시 : 8비트 데이터가 저장되어야하는데 9비트 이상의 데이터가 들어온 경우 이를 다시 8비트로 제한할때 (a & 255
-> a & 11111111
)연산자 | 설명 | 예시 | 결과 |
---|---|---|---|
= | 할당 | a = b | b의 값을 a에 할당 |
+= | 더한 후 할당 | a += b | a에 b를 더한 값을 a에 할당 |
-= | 뺀 후 할당 | a -= b | a에서 b를 뺀 값을 a에 할당 |
*= | 곱한 후 할당 | a *= b | a에 b를 곱한 값을 a에 할당 |
/= | 나눈 후 할당 | a /= b | a를 b로 나눈 값을 a에 할당 |
%= | 나머지를 구한 후 할당 | a %= b | a를 b로 나눈 나머지를 a에 할당 |
자바는 항상 변수의 값을 복사해서 할당한다
는 원칙입니다.Assignment.java
package operators;
public class Assignment {
public static void main(String[] args) {
// 1. 할당 연산자
int a = 10;
System.out.println("a = " + a); // 10
int b = a;
int c = 2 * b;
System.out.println("a = " + a); // 10
System.out.println("b = " + b); // 10
System.out.println("c = " + c); // 20
System.out.println("-----------------------");
// 2. 복합 대입 연산자
int d = 10;
System.out.println("d = " + d); // 10
System.out.println("d += 5 : " + (d += 5)); // 15
System.out.println("d -= 5 : " + (d -= 5)); // 10
System.out.println("d *= 5 : " + (d *= 5)); // 50
System.out.println("d /= 4 : " + (d /= 4)); // 12
System.out.println("d %= 7 : " + (d %= 7)); // 5
System.out.println("d = " + d); // 5
}
}
Assignment.class
a = 10
a = 10
b = 10
c = 20
-----------------------
d = 10
d += 5 : 15
d -= 5 : 10
d *= 5 : 50
d /= 4 : 12
d %= 7 : 5
d = 5
자바는 항상 변수의 값을 복사해서 할당한다
는 원칙에 의한 것입니다.d = d + 5
-> d += 5
연산자 | 설명 | 예시 | 결과 |
---|---|---|---|
? : | 조건 ? 참일 때의 값 : 거짓일 때의 값 | int result = (a > b) ? a : b; | a가 b보다 크면 a, 그렇지 않으면 b |
연산자 | 설명 | 예시 | 결과 |
---|---|---|---|
instanceof | 객체가 특정 클래스의 인스턴스인지 확인 | if (obj instanceof String) | obj가 String의 인스턴스이면 true |
new | 객체를 생성 | MyClass obj = new MyClass(); | MyClass의 새로운 인스턴스를 생성 |
. | 멤버 접근 | obj.method() | obj의 method 메서드를 호출 |
[] | 배열 요소 접근 | arr[0] | 배열 arr의 첫 번째 요소를 반환 |
String name = "Alice";
boolean isString = name instanceof String; // isString은 true
()
로 변환 시킬 타입을 명시하여 실시할 수 있습니다.Casting.java
package operators;
public class Casting {
public static void main(String[] args) {
long l = 2147483648L;
float f = 3.14F;
// 1. 자동 형변환
double d = l + f; // long + float -> float + float -> double + float -> double
System.out.println("d = " + d); // 2147483651.14
// 2-1. 강제 형변환 (오버플로우)
int i = (int)l; // long -> int
System.out.println("i = " + i); // -2147483648 (오버플로우)
// 2-2. 강제 형변환 (정확한 값)
int i2 = (int)f; // float -> int
System.out.println("i2 = " + i2); // 3
}
}
Casting.class
d = 2.147483648E9
i = -2147483648
i2 = 3
잘 봤습니다.