Hi all, I have encountered a strange piece of code whic goes like: ============================================================= final long A = 24*60*60*1000*1000; final long B = 24*60*60*10000; System.out.println("A/B = "+A/B); ============================================================ This gives "5" as against the expected value of "1000". Can anybody let me know how this "5" has come? Thanks, Naresh
MAX VALUE of Integer = 2147483647 MIN VALUE of Integer = -2147483648 Value of A as Integer = 86400000000, it is more than what an integer can hold. So it rotates the bits, so current value is 24 * 60 * 60 * 1000 * 1000 = 500654080, instead of 86400000000 A = 500654080 B = 864000000 A/B = 0, not 5 !
Reason : When the numbers are participating in arithmatic operations, it will be converted to the type with highest capacity. Here all the participants are of int type. Hence multiplication and division happens on int; the LHS accepts it since long is a bigger type than iint, but does not have any role in operations.
As Matt Caripto suggested if you give 1000L and 10000L respectively, the arithmatic opertions will work in long type and A will be able to hold the full value of 86400000000. Because MAX VALUE of Long = 9223372036854775807 MIN VALUE of Long = -9223372036854775808 Value of A as Long = 86400000000