산술연산
사칙연산
+ - * / %
int result = 10 + 20 - 30 * 40 / 50 % 60;
// 곱하기와 나누기(나머지)가 우선순위가 높음
복합 연산자
+=, -=, *=, /=, %=
변수에 저장되어 있는 값에 연산을 수행할때 수행할 연산자와 대입연산자를 결합해서 사용.
result += 3;
//reuslt = reuslt - 2 * 3;
result -= 2 * 3;
증감연산자
++, —
변수의 값을 1씩 증가 시키거나, 감소시킨다.
int i = 0;
System.out.println(++i); // 1
System.out.println(i++); // 1
System.out.println(--i); // 1
System.out.println(i--); // 1
- 전위형(++i)
변수의 값을 읽어오기 전에 1 증가된다. - 후위형(i++)
변수의 값을 읽어온 후에 1 증가된다.
주의사항
피연산자의 타입이 서로 같아야만 연산이 가능하다.
// 표현 범위가 큰 타입으로 강제 형변환 됨
int _int = 10;
double _double = 3.14;
double result2 = ~~(double)~~_int + _double;
System.out.println(result2); // 13.14
// int보다 작은 타입은 int로 형변환 된다
byte _byte = 5;
short _short = 10;
int result3 = _byte + _short;
System.out.println(result3);
오버플로우, 언더플로우
// 오버플로우
byte b = 127; // byte의 최대값
b++;
System.out.println(b); // -128
// 언더플로우
byte b = -128; // byte의 최소값
b--;
System.out.println(b); // 127반응형
'IT > JAVA' 카테고리의 다른 글
| [JAVA] 논리연산 (0) | 2020.06.25 |
|---|---|
| [JAVA] 비교연산 (0) | 2020.06.25 |
| [JAVA] 입력, 출력 (0) | 2020.06.24 |
| [JAVA]변수 , 상수 (0) | 2020.06.24 |
| [JAVA]자료형 (0) | 2020.06.24 |