C_Arithmetic.java
package com.kh.operator;
// 산술연산자 (+ - * / %)
public class C_Arithmetic {
public void method1() {
int num1 = 10;
int num2 = 3;
//우선순위 : (* / %) > (+ -)
System.out.println("num1 + num2 = " + (num1+num2));
System.out.println("num1 - num2 = " + (num1-num2));
System.out.println("num1 * num2 = " + num1*num2);
System.out.println("num1 / num2 = " + num1/num2);
System.out.println("num1 % num2 = " + num1 % num2);
}
public void method2() {
int a = 5;
int b = 10;
int c = (++a) + b; // a=6, b=10, c=16
int d = c / a; // a=6, b=10, c=16, d=2
int e = c % a; // a=6, b=10, c=16, d=2 e=4
int f = e++; // a=6, b=10, c=16, d=2 e=4(5) f=4
int g = (--b) + (d--); //a=6, b=9, c=16 d=2(1) e=5 f=4 g=11
int h = 2; // a=6, b=9, c=16 d=2(1) e=5 f=4 g=11 h=2
int i = a++ + b / (--c / f) * (g-- - d) % (++e + h); //a=6(7), b=9, c=15, d=1 e=6 f=4 g=11(10) h=2 i=
// 6 + 9 / 3 * 10 % 8
// 6 + 3 * 10 % 8
// 6 + 30 % 8
// 12
// a=7, b=9, c=15, d=1, e=6, f=4, g=10, h=2, i=12
System.out.println("a : " + a);
System.out.println("b : " + b);
System.out.println("c : " + c);
System.out.println("d : " + d);
System.out.println("e : " + e);
System.out.println("f : " + f);
System.out.println("g : " + g);
System.out.println("h : " + h);
System.out.println("i : " + i);
}
}
Point
- 후증감되는게 헷갈릴 수 있으니 주의 처리숫자 옆에 ()로 숫자 넣어 주기 ex. a=3(4)
'JAVA > JAVA 기초' 카테고리의 다른 글
JAVA 기초 - 논리연산자(Logical Operator) (0) | 2020.05.31 |
---|---|
JAVA 기초 - 비교연산자(Comparison Operator) (0) | 2020.05.31 |
JAVA 기초 - 증감연산자(Increase & decrease Operators) (0) | 2020.05.31 |
JAVA 기초 - 논리부정연산자(Logical Negation) (0) | 2020.05.31 |
JAVA 기초 - Printf (0) | 2020.05.29 |