I know that a data type is automatically promoted to the upper data type byte-short-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);
}
}
Output:
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
My question is that why b*b promote to int because 42+42=84 and byte range is -128 to 127. 84 is in range. Furthermore, why
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);
this line got 0 why not promote that to double.
my question is that why b*b promote to int
Because that's what the language specification says it will do.
and why [...] this line got 0 why not promote to that double
Again, because that's not how the language is defined.
Read section 15.17 of the JLS:
The multiplicative operators have the same precedence and are syntactically left-associative (they group left-to-right).
The type of each of the operands of a multiplicative operator must be a type that is convertible (§5.1.8) to a primitive numeric type, or a compile-time error occurs.
Binary numeric promotion is performed on the operands (§5.6.2).
Binary numeric promotion (5.6.2) will promote byte
operands to int
, so you end up with int * int
arithmetic in all cases in your code. In the first case you've got byte * byte
so both operands are promoted to int
; for the long line you've got one byte * byte
and the rest are int * byte
, where just the second operand is promoted to int
. That choice is made at compile time, and has nothing to do with the values at execution time - the JVM doesn't decide to promote values to double
because they overflow the bounds of int
.
See more on this question at Stackoverflow