Java basic data Type auto-Transform overflow problem

Source: Internet
Author: User

<span style= "FONT-SIZE:14PX;" >public class Testcast {public static void main (string[] args) {int MONEY=1000000000;//10 billion int year=20;int total=money* Year SYSTEM.OUT.PRINTLN (total);}} </span>

Output: 1474836480

this time we can think of the range of int is -2.1 billion ~21 billion , and Money*year results obviously beyond this range , so will overflow, But what about changing the data type of total to long?

public class Testcast {public static void main (string[] args) {int MONEY=1000000000;//10 billion int year=20;long Total=money*yea R SYSTEM.OUT.PRINTLN (total);}}
output results: -1474836480

In theory, the correct results should be output, but the actual or overflow. What is this for? Because money and year are int, so the product of the two is also int type, beyond the int type will overflow, how can we solve it? Cast money or year into a long type, as follows:

<span style= "FONT-SIZE:14PX;" >int MONEY=1000000000;//10 million int year=20;long total= (long) money*year; System.out.println (total);</span>
output Result: 20000000000
This outputs the correct result. However, if it is written as Long total= (long), the result is still incorrect, because the product of money and year has been overrun and then cast to a long type, the result is still incorrect.


Let's look at a small example:

public class Testcast {public static void main (string[] args) {/*int MONEY=1000000000;//10 billion int year=20;long total=money*y Ear SYSTEM.OUT.PRINTLN (total); *//*int MONEY=1000000000;//10 billion int year=20;long total= (long) money*year; SYSTEM.OUT.PRINTLN (total); *///How many times a person's heart beats in 70 long times=70*60*24*365*60; System.out.println (Times);}}
Output Result:-2087447296

Obviously overflow, how should we deal with it? In this statement long times=70*60*24*365*60; , cast to a long type by adding L to any number. such as long times=70*60*24*365*60l; tip, add L after the first number, so that the preceding multiplication is prevented from spilling as follows: long times=70l*60*24*365*60;


--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- ---------

Summarize

Before a problem often encountered this large number of problems, sometimes can be correct sometimes need to debug several times to be correct, now has thoroughly understood the large number of forced conversion overflow problem, hoping to help the friends do not understand.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java basic data Type auto-Transform overflow problem

Related Article

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.