An invisible Java int Overflow

Source: Internet
Author: User

Background:

I recently worked on an SNS-like project where I used MongoDB to store feed information and used a timer to delete expired information.
The timer time variable is 60 days, set
Java code

  1. Private long msginvaliddue = 60*24*3600*1000;


The reason for writing constants is to make the code easy to read and maintain.

The occurrence of the story:
One day when the project went live, it suddenly found that the data lost two weeks ago, traversed the program, and did not find any logic problems, I even began to worry about data loss caused by MongoDB (which has happened in the industry ). I was struggling ~~
However, the next day, we found that data loss was sustained and regular, So we focused on "scheduled deletion tasks ". No problem is found in the left check and right check. You can trace and debug the problem step by step ....

End of the story:
It is hard to imagine that the problem occurs on the long constant msginvaliddue. In principle, its value should be 51840010000l (60 days), but its actual value is 889032704 (about 10 days), why ???? It is actually an overflow of int in the calculation process ~~~~ Too concealed bug ~~ Correct writing
Java code

  1. Private long msginvaliddue = 60*24 * 3600l * 1000;


Add L to any constant and convert it to the long type ~~~ This time, it's really a trash ~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.