class LongDiv{
public static void main(String [] args){
final long x = 24*60*60*1000*1000;
final long y = 24*60*60*1000;
System.out.println(x/y);
}
}
although the expected answer is 1000, but the javac gives it as 5. Reason?
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.
The long
xyou are creating isn’t the value you expected. It is in the integer range. To create longs, use:The
xyou computed, in the integer range, was500654080. This divided by they( =86400000), results in5.794607407407407.... Java truncates the decimal part which causes the 5.By adding an
Lafter the number literal, you tell the compiler to compile it as alonginstead of anint. The value forxyou expected is86400000000. But is was compiled as an int.We can reproduce the wrong value for
x(500654080) by truncating it to an int: