简体   繁体   English

解析长到负数

[英]parse long to negative number

code: 码:

public class Main{
    public static void main(String[] a){
        long t=24*1000*3600;
        System.out.println(t*25);
        System.out.println(24*1000*3600*25);
    }
}

This prints : 打印:

2160000000

-2134967296

Why? 为什么?


Thanks for all the replies. 感谢所有的答复。

Is the only way to use L after the number? 是在数字后使用L的唯一方法吗?

I have tried the (long)24*1000*3600*25 but this is also negative. 我已经尝试过(long)24*1000*3600*25但这也是负面的。

You reached the max of the int type which is Integer.MAX_VALUE or 2^31-1. 您已达到int类型的最大值,即Integer.MAX_VALUE或2 ^ 31-1。 It wrapped because of this, thus showing you a negative number. 因此,它被包裹了,因此显示为负数。

For an instant explanation of this, see this comic: 有关此内容的即时说明,请参见此漫画:

替代文字

To explain it clearly, 为了清楚地解释,

System.out.println(24*1000*3600*25);

In the above statement are actually int literals. 在上面的语句中实际上是int文字。 To make treat them as a long literal you need to suffix those with L . 要将它们视为long字面量,您需要在L后缀后缀。

System.out.println(24L*1000L*3600L*25L);

Caveat, a small l will suffice too, but that looks like capital I or 1 , sometimes. 请注意,一个小的l也足够,但是有时看起来像大写的I1 Capital I doesn't make much sense here, but reading that as 1 can really give hard time. 资本I在这里没有多大意义,但是读为1确实会给你带来困难。 Furthermore, Even sufficing a single value with L will make the result long . 此外,即使L满足单个值也将使结果long

In the first case you are printing a long but in the second, you are printing it as int . 在第一种情况下,您将打印很long而在第二种情况下,您将其打印为int

And int has a range from: -2^31 to 2^31 - 1 which is just below what you are calculating (int max: 2147483647 you: 2160000000) so you overflow the int to the negative range. 而且int的范围是:-2 ^ 31到2 ^ 31-1,它正好在您要计算的范围以下(int max:2147483647您:2160000000),因此您会将int溢出到负数范围。

You can force the second one to use long as well: 您也可以强制第二个使用long

System.out.println(24L*1000*3600*25);

You should suffix the numbers with 'l'. 您应该在数字后加上'l'。 Check the snippet below: 检查以下代码段:

   public static void main(String[] a){
        long t=24*1000*3600;
        System.out.println(t*25);
        System.out.println(24l*1000l*3600l*25l);
    }

Integral literals are treated as type int by default. 默认情况下,积分文字被视为int类型。 24*1000*3600*25 is greater than Integer.MAX_VALUE so overflows and evaluates to -2134967296. 24*1000*3600*25大于Integer.MAX_VALUE因此会溢出并评估为-2134967296。 You need to explicitly make one of them a long using the L suffix to get the right result: 您需要使用L后缀明确地使其中之一变long ,以得到正确的结果:

System.out.println(24L*1000*3600*25);

If you want to do mathematical operations with large numerical values without over flowing, try the BigDecimal class. 如果要使用大数值进行数学运算而又不会溢出,请尝试使用BigDecimal类。

Let's say I want to multiply 假设我想乘

200,000,000 * 2,000,000,000,000,000,000L * 20,000,000 200,000,000 * 2,000,000,000,000,000,000L * 20,000,000

int testValue = 200000000;
System.out.println("After Standard Multiplication = " +
                                                       testValue * 
                                                       2000000000000000000L * 
                                                       20000000);

The value of the operation will be -4176287866323730432, which is incorrect. 该操作的值将是-4176287866323730432,这是不正确的。

By using the BigDecimal class you can eliminate the dropped bits and get the correct result. 通过使用BigDecimal类,您可以消除丢失的位并获得正确的结果。

int testValue = 200000000;        
System.out.println("After BigDecimal Multiplication = " +
                              decimalValue.multiply(
                              BigDecimal.valueOf(2000000000000000000L).multiply(
                              BigDecimal.valueOf(testValue))));

After using the BigDecimal, the multiplication returns the correct result which is 使用BigDecimal之后,乘法运算会返回正确的结果,即

80000000000000000000000000000000000 80000000000000000000000000000000000000000

(int)Long.valueOf("2345678901").longValue();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM