을 두배로 승격되지왜 내 데이터 형식이 자동으로 I는 데이터 유형이 자동으로 짧은 INT 바이트 상위 데이터 유형으로 승진 것을 알고
class Temp {
void check(byte x) {
System.out.println(x + " is the byte type");
}
void check(short x) {
System.out.println(x + " is the short type");
}
void check(int x) {
System.out.println(x + " is the int type");
int y = x;
System.out.println(y + " is the int type");
}
void check(long x) {
System.out.println(x + " is the long type");
}
void check(float x) {
System.out.println(x + " is the float type");
}
void check(double x) {
System.out.println(x + " is the double type");
}
public static void main(String args[]) {
byte b = 42;
char c = 'a';
short s = 1024;
int i = 50000;
float f = 5.67f;
double d = .1234;
double result = (f * b) + (i/c) - (d * s);
System.out.println((f * b) + " + " + (i/c) + " - " + (d * s));
System.out.println("result =" + result);
Temp t = new Temp();
t.check(f * b);
t.check(i/c);
t.check(d * s);
t.check(b + b);
t.check(b * b);
t.check(b * b * b);
t.check(b * b * b * b * b * b * b * b * b);
t.check(b * b * b * b * b * b * b * b * b * b * b * b * b * b * b * b
* b * b * b * b * b * b * b * b * b * b * b * b * b * b * b);
t.check(b * b * b * b * b * b * b * b * b * b * b * b * b * b * b * b
* b * b * b * b * b * b * b * b * b * b * b * b * b * b * b * b
* b * b * b * b * b);
}
}
출력 :
238.14 + 515 - 126.3616
result =626.7784146484375
238.14 is the float type
515 is the int type
515 is the int type
126.3616 is the double type
84 is the int type
84 is the int type
1764 is the int type
1764 is the int type
74088 is the int type
74088 is the int type
-1889539584 is the int type
-1889539584 is the int type
-2147483648 is the int type
-2147483648 is the int type
0 is the int type
0 is the int type
내 질문은 42 * 42 = 84이고 바이트 범위가 -128 ~ 127이기 때문에 b * b가 int로 승격시키는 이유입니다. 84가 범위 내에 있습니다. 또한, 이유는
t.check(b * b * b * b * b * b * b * b * b * b * b * b * b * b * b * b
* b * b * b * b * b * b * b * b * b * b * b * b * b * b * b);
이 줄을 가지고있어 왜 두 배로 홍보하지.
"왜 b * b가 int로 승격되는지"- 그렇지 않습니다. 그것은 승진 된'b'입니다. –