java 将 long 解析为负数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3957937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:08:31  来源:igfitidea点击:

parse long to negative number

javamathnumberslong-integer

提问by hguser

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*25but this is also negative.

我已经尝试过,(long)24*1000*3600*25但这也是负面的。

回答by Alberto Zaccagni

You reached the max of the inttype which is Integer.MAX_VALUEor 2^31-1. It wrapped because of this, thus showing you a negative number.

您达到了或 2^31-1int类型的最大值Integer.MAX_VALUE。它因此被包裹,从而向您显示负数。

For an instant explanation of this, see this comic:

有关此问题的即时解释,请参阅此漫画:

alt text

替代文字

回答by Adeel Ansari

To explain it clearly,

为了解释清楚,

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

In the above statement are actually intliterals. To make treat them as a longliteral you need to suffix those with L.

在上面的语句中实际上是int文字。要将它们视为long文字,您需要为它们添加后缀L.

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

Caveat, a small lwill suffice too, but that looks like capital Ior 1, sometimes. Capital Idoesn't make much sense here, but reading that as 1can really give hard time. Furthermore, Even sufficing a single value with Lwill make the result long.

警告,一个小的l也足够了,但这看起来像资本,I或者1,有时。资本I在这里没有多大意义,但阅读它1真的很难。此外,即使有足够的单个值L也会使结果long.

回答by Lasse Espeholt

In the first case you are printing a longbut in the second, you are printing it as int.

在第一种情况下,您正在打印 a,long但在第二种情况下,您将其打印为int.

And inthas 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 longas well:

您也可以强制使用第二个long

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

回答by Sagar V

You should suffix the numbers with 'l'. Check the snippet below:

您应该在数字后加上“l”。检查下面的片段:

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

回答by dogbane

Integral literals are treated as type intby default. 24*1000*3600*25is greater than Integer.MAX_VALUEso overflows and evaluates to -2134967296. You need to explicitly make one of them a longusing the L suffix to get the right result:

int默认情况下,整型文字被视为类型。24*1000*3600*25大于Integer.MAX_VALUEso 溢出并计算为 -2134967296。您需要long使用 L 后缀明确地将其中一个设为 a以获得正确的结果:

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

回答by dinidu

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

80000000000000000000000000000000000

回答by Honghe.Wu

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