アジットDhdharia:
byte byteChallenge = 68;
short shortChallenge = 190;
int intChallenge = 2147483647;
long longChallenge = (50000L + 10L * (byteChallenge + shortChallenge + intChallenge));
long longChallengeX = 50000L + 10L * ((long) byteChallenge + (long) shortChallenge + (long)
intChallenge);
System.out.println(longChallenge);
System.out.println(longChallengeX);
私は取得しています出力は次のようになります。
-21474783910
21474889050
チャールズDowbecki:
トリッキーな部分は、int intChallenge = 2147483647
正の最大値である定数int
(参照しますInteger.MAX_VALUE
)。
あなたが実行するとbyteChallenge + shortChallenge + intChallenge
、それはありint
、その結果算術文整数オーバーフロー。あなたの例では、整数オーバーフローの結果が負の数です。
あなたが実行すると(long) byteChallenge + (long) shortChallenge + (long) intChallenge
、それはあるlong
算術。以来long
サポートよりもはるかに大きい値int
オーバーフローが存在しません。