Javaの四則演算時の型変換のルール
Javaプリミティブの数値同士の計算で、片方または両方のオペランドがどの型に変換されるのかはルールがある (このルールによって結果の型も決定される)。
If either operand is of type double, the other is converted to double.
code: java
jshell> 7 / 3.0d
$17 ==> 2.3333333333333335
Otherwise, if either operand is of type float, the other is converted to float.
code: java
jshell> 7 / 3.0f
$18 ==> 2.3333333
Otherwise, if either operand is of type long, the other is converted to long.
code: java
jshell> Integer.MAX_VALUE
$20 ==> 2147483647 // intの最大値
jshell> Integer.MAX_VALUE + 1
$21 ==> -2147483648 // intを足した場合はオーバーフローしてしまう
jshell> Integer.MAX_VALUE + 1L
$22 ==> 2147483648 // longに変換されるので、intの最大値+1の数値を得られる
Otherwise, both operands are converted to type int.
つまり優先順位としては、
double > float > long > int (and others)
となる。
上記のルールには、その他でまとめられてしまっているがshortとかの型はintに変換されて計算される。