Note the value range of Java int Integer Variables

Source: Internet
Author: User

Let's first make an example: the current time is 10:27:01. 89

// 24*60*60*1000 indicates the number of milliseconds in a day. The value of 24 indicates 24 days. <br/> long daylong = 24*24*60*60*1000; <br/> // system. currenttimemillis ()-daylong indicates the current time minus the 24-day time <br/> timestamp T = new timestamp (system. currenttimemillis ()-daylong); <br/> // print time <br/> system. out. println (t );


Program output: 10:27:01. 89

When the first 24 in daylong is changed to 25, guess what the output is? 2011-04-23 10:27:01. 89
?

The program output is 03:27:01. 89.
Why?

This is because 25*24*60*60*1000 is out of the integer int range. in Java, the int value range is-2 ^ 31 ~ 2 ^ 31-1
That is,-2147483648 ~2147483647

In the int value range, 24*24*60*60*1000 = 2073600000
While 25*24*60*60*1000 =-2134967296
!

Solution: force type conversion to long type, long daylong = (long) 25*24*60*60*1000
; Daylong = 2160000000

Program output: 10:27:01. 89

 

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.