우리가 일반적으로 알고 쓰는 연산에 사용되는 산술 연산자
+ - / *
사칙연산을 기본으로 한다
num1 += 50; // num1 = num1 + 50 (80)
num2 -= 10; // num2 = num2 - 10; (40)
num3 *= 20; // num3 = num3 * 20; (400)
num4 /= 10; // num4 = num4 / 10; (1)
위와 같은 경우는 대입연산자
https://yhmane.tistory.com/113
| &
|
하나의 비트를 비교하더라도 1이 있으면 1
&
두 비트 모두 1이 있어야 1
|| &&
||
하나라도 true이면 true
&&
두 개 모두 true여야 true
public static void main(String[] args){
int a = 3;
int b = 4;
int c = 4;
if(a==b && ++a==b){
System.out.println("논리연산자:ture");
}
else{
System.out.println("논리연산자:false");
}
//false
if(a==b && ++a==b){
System.out.println("비트연산자:true");
}
else{
System.out.println("비트연산자:false");
}
//false
if(c==b && c==b){
System.out.println("논리연산자:ture");
}
else{
System.out.println("논리연산자:false");
}
//true
if(c==b && c==b){
System.out.println("비트연산자:true");
}
else{
System.out.println("비트연산자:false");
}
//true
}
논리연산자와 비트연산자의 값이 같게 나오는데
이 때문에 boolean타입만 조건으로 사용할 수 있는 자바의 if문에서 |과&을 사용할 수 있다.
비트연산자 : if문에서는 피연산자를 무조건 앞뒤 다 수행
앞에 값이 true, false 상관없이 뒤에 ++a==b를 수행
논리연산자 : 앞의 피연산자에 따라 ||, &&에 따라서 뒤의 피연산자를
실행할 수도, 안할 수도 있다.
https://it-jin-developer.tistory.com/9
두 피연산자 사이의 대소나 동등의 정도 측정
< > ==
public static void main(String[] args) {
A a = new A();
B b = new B();
//객체 a는 자기 자신의 객체이기 때문에 형변환 가능.
System.out.println(a instanceof A); //true 출력
//객체 b는 A의 자식객체이기 때문에 A로 형변환 가능.
System.out.println(b instanceof A); //true 출력
//객체 a는 B의 부모객체이기때문에 형변환 불가능.
System.out.println(a instanceof B); //false 출력
//객체 b는 자기 자신의 객체이기때문에 형변환 가능.
System.out.println(b instanceof B); //true 출력
} // end of main
} // end of class
class A{ }
class B extends A{ }
할당 연산자
변수에 값을 지정(할당)할 때 사용
= : 단순 할당 연산자
그 외 모두 복합 할당 연산자
https://codetorial.net/cpp/operators/assignment_operator.html
java 8버전부터 추가
람다 표현식의 일부
메서드를 변수처럼 다룰 수 있다는 편리함
클래스에 종속되지 않음
(조건) : true일 경우 ? false일 경우
최단산쉬관리삼대
최우선연산자 -> 단항 연산자(항 1개) -> 이항 연산자(항 2개)
-> 산술 연산자 -> 쉬프트 연산자 -> 관계 연산자 -> 논리 연산자
-> 삼항 연산자(항 3개) -> 대입 연산자
https://yoonhihi.tistory.com/18
public class SwitchExpressionsExample {
public static void main(String[] args) {
SwitchExpressionsExample example = new SwitchExpressionsExample();
example.printDay(Day.FRI);
example.printDay(Day.TUE);
example.printDay(Day.SUN);
}
public void printDay(Day today) {
switch (today) {
case MON:
case TUE:
case WED:
case THUR:
case FRI:
System.out.println(today.name() + " is Weekday");
break;
case SAT:
case SUN:
System.out.println(today.name() + " is Weekend");
break;
}
}
enum Day {
MON, TUE, WED, THUR, FRI, SAT, SUN
}
}
'break' 위치에 따라 개발자의 실수인지, 의도적인지 알아차리기 어려울 경우가 있다
public void printDay(Day today) {
switch (today) {
case MON, TUE, WED, THUR, FRI -> System.out.println(today.name() + " is Weekday");
case SAT, SUN -> System.out.println(today.name() + " is Weekend");
}
}
',' 로 구분해서 한 번에 처리 가능
단일 실행이면 ->를 사용할 수 있고, 다중 실행이면 {} 구문으로 만들 수 있음
-> 대신 : 을 사용해서 예전 방식으로도 사용 가능