hello I have some question about java, why the following code return strange value?
System.out.println("Strange " + (20 * 232792560)/20);
why do I recieve 18044195?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Because
(20 * 232792560)overflows the range of anint, and wraps round the number range several times to become360883904. That is then divided by20to give you the result that you see.If you want the correct result, then you need to do:
(Marking a literal with an
Lmeans that the constant maths will be done withlong, rather than withint, so this will no longer overflow.)