将 long 解析为负数

发布于 2024-09-28 09:37:12 字数 396 浏览 3 评论 0原文

代码:

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);
    }
}

这打印:

2160000000

-2134967296

为什么?


感谢您的所有回复。

是在数字后面使用 L 的唯一方法吗?

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

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?

I have tried the (long)24*1000*3600*25 but this is also negative.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

柏林苍穹下 2024-10-05 09:37:12

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

有关此内容的即时说明,请参阅此漫画:

alt text

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

For an instant explanation of this, see this comic:

alt text

烟花易冷人易散 2024-10-05 09:37:12

为了解释清楚,

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

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

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

需要注意的是,一个小的 l 也足够了,但有时看起来像大写的 I1。大写的 I 在这里没有多大意义,但是将其读作 1 确实会很困难。此外,即使使用 L 满足单个值也会使结果long

To explain it clearly,

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

In the above statement are actually int literals. To make treat them as a long literal you need to suffix those with L.

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

Caveat, a small l will suffice too, but that looks like capital I or 1, sometimes. Capital I doesn't make much sense here, but reading that as 1 can really give hard time. Furthermore, Even sufficing a single value with L will make the result long.

奶气 2024-10-05 09:37:12

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

int 的范围为:-2^31 到 2^31 - 1,它正好低于您正在计算的值(int max:2147483647 you:2160000000),因此您将 int 溢出到负范围。

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

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

In the first case you are printing a long but in the second, you are printing it as 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.

You can force the second one to use long as well:

System.out.println(24L*1000*3600*25);
还在原地等你 2024-10-05 09:37:12

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

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

You should suffix the numbers with '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);
    }
一生独一 2024-10-05 09:37:12

默认情况下,整数文字被视为 int 类型。 24*1000*3600*25 大于 Integer.MAX_VALUE,因此会溢出并计算为 -2134967296。您需要使用 L 后缀显式地将其中之一设为 long 才能获得正确的结果:

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

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

System.out.println(24L*1000*3600*25);
不爱素颜 2024-10-05 09:37:12

如果您想对大数值进行数学运算而不发生溢出,请尝试使用 BigDecimal 类。

假设我想乘以

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);

运算的值将是 -4176287866323730432,这是不正确的。

通过使用 BigDecimal 类,您可以消除丢失的位并获得正确的结果。

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

使用 BigDecimal 后,乘法返回正确的结果,即

80000000000000000000000000000000000

If you want to do mathematical operations with large numerical values without over flowing, try the BigDecimal class.

Let's say I want to multiply

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.

By using the BigDecimal class you can eliminate the dropped bits and get the correct result.

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

80000000000000000000000000000000000

鹿港小镇 2024-10-05 09:37:12
(int)Long.valueOf("2345678901").longValue();
(int)Long.valueOf("2345678901").longValue();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文