public static void main(String[] args) {
int a = 1_000_000; // 1,000,000 1백만
int b = 2_000_000; // 2,000,000 2백만
long c = a * b; // a * b = 2,000,000,000,000 ?
System.out.println(c);
}
int * int 연산과정에서 오버플로우가 일어난다
public static void main(String[] args) {
long a = 1_000_000 * 1_000_000;
long b = 1_000_000 * 1_000_000l;
System.out.println("a="+a);
System.out.println("b="+b);
}
b는 int * long 으로 인해 자연스레 형변환이 선행된 연산이 이루어진다.
public static void main(String[] args) {
int a = 1000000;
int result1 = a * a / a;
int result2 = a / a * a;
System.out.printf("%d * %d / %d = %d%n", a, a, a, result1);
System.out.printf("%d / %d * %d = %d%n", a, a, a, result2);
}
result1의 경우
1000000 * 1000000 / 1000000
=-727379968 / 1000000 // 자료형이 int이기 때문에 오버플로우가 발생한 채로 계산한다!!
=-727
public static void main(String[] args) {
char a = 'a';
char d = 'd';
char zero = '0';
char two = '2';
System.out.printf("'%c' - '%c' = %d%n", d, a, d - a);
System.out.printf("'%c' - '%c' = %d%n", two, zero, two - zero);
System.out.printf("'%c' = %d%n", a, (int)a);
System.out.printf("'%c' = %d%n", d, (int)d);
System.out.printf("'%c' = %d%n", zero, (int)zero);
System.out.printf("'%c' = %d%n", two, (int)two);
}
문자도 유니코드(부호없는 정수) 형태로 저장되기때문에 사칙연산이 가능하다.