Java int 到 long 赋值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24755629/
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-08-14 14:42:53  来源:igfitidea点击:

int to long assignment

javaintlong-integer

提问by Shashish Chandra

I have been trying this int and long conversion where I have tried assigning an intvariable to a longvariable. The code is as follows:

我一直在尝试这种 int 和 long 转换,我尝试将int变量分配给long变量。代码如下:

public static void main(String []args){
    int i = 1024;
    long j = i;
    long k = i*i*i*i;
    System.out.println("j= " +j+ ", k= " +k);
}

So, while printing j, it simply gave an output of 1024. But while printing k, it showed an overflow (k=0). I sorted out this problem by using this technique, where I have explicitly casted each ito long. i.e.

因此,在打印 时j,它只是给出了1024. 但是在打印时k,它显示溢出(k=0)。我通过使用这种技术解决了这个问题,我已经明确地将每个转换ilong. IE

public static void main(String []args){
    int i = 1024;
    long j = i;
    long k = ((long)i)*((long)i)*((long)i)*((long)i);
    System.out.println("j= " +j+ ", k= " +k);
}

Now, it showed correct values of both jand k. So, I wanted to know why is this needed to cast intin the second case but not in the first one. k, being a longcan retain this value after this heavy assignment. But, why it is not been correctly assigned?

现在,它显示了j和 的正确值k。所以,我想知道为什么需要int在第二种情况下而不是在第一种情况下进行转换。k,作为 along可以在这个繁重的赋值之后保留这个值。但是,为什么它没有被正确分配?

采纳答案by dav_i

The reason is that your line

原因是你的线路

long k = i*i*i*i;

can be thought of as

可以认为是

long k = ((i*i)*i)*i;

or...

或者...

int k1 = i*i;
int k2 = k1*i;
int k3 = k2*i;
long k = k3;

So when any of knoverflows, you get the error.

因此,当任何kn溢出时,您都会收到错误消息。

When you do your casts you're obviously circumventing this problem by always multiplying longs together.

当你做你的演员表时,你显然是通过总是将longs相乘来规避这个问题。

Of course the simplest modification to your initial program is to define ias a longstraight away, instead of an int.

当然,你的初始程序最简单的修改是定义i为一个long直线距离,而不是一个int

long i = 1024L;

回答by Sanjeev

i*i*i*iis an all int multiplication so the resultant is integer only (Which is overflowing in your case). for a long resultant you only need to convert one of them to long so this is sufficient

i*i*i*i是全整型乘法,因此结果仅为整数(在您的情况下溢出)。对于长结果,您只需要将其中一个转换为 long 这样就足够了

long k = ((long)i)*i*i*i;

回答by Suresh Atta

In first the value of i is an int and the integer multipllication happening and i*i*i*iresult is integer and hence the integer overflow.

首先, i 的值是一个 int 并且整数乘法发生并且i*i*i*i结果是整数,因此integer overflow.

Where as in second case you are explicitly telling/making the result as long.

在第二种情况下,您明确告知/将结果设为long.

回答by CoderCroc

1099511627776which is the result of multiplication is >Integer.MAX_VALUEas it's Integer Multiplication so after i*i*ivalue would be 1073741824but after multiplication with 1024it becomes out of range which causes integer overflowand k will be 0.

1099511627776这是乘法的结果,>Integer.MAX_VALUE因为它是整数乘法,所以在i*i*i值之后1073741824但乘以1024它后变得超出范围,这会导致整数溢出并且 k 将为 0。

  • You can cast one of the ito long
  • Multiply with 1L(1L*i*i*i*i) But NOT i*i*i*i*1L
  • You can also assign i*i*ito long and than multiply with ilike this long k =(k=i*i*i)*i;
  • 您可以将其中之一投射ilong
  • 乘以1L( 1L*i*i*i*i) 但不i*i*i*i*1L
  • 您也可以分配i*i*i给 long ,而不是i像这样乘以long k =(k=i*i*i)*i;

回答by Ranic

This problem is based on the fact that different types use different amount of memory.int and long are both Integers, the difference is that int is 4 bytes, while long is 8 bytes.

这个问题是基于不同类型使用不同数量的内存的事实int 和 long 都是整数,区别在于int 是 4 bytes,而long 是 8 bytes

Lets modify your code a bit:

让我们稍微修改一下你的代码:

public class Test {


  public static void main(String []args){
        int i = 1024;
        long j = i;
        long k = i*i*i;

        System.out.println("My multipliers are of type int:");
        System.out.println("j= " +j+ ", k= " +k);           
        System.out.println("Is k less than Integer.MAX_VALUE: "  + (k < Integer.MAX_VALUE? "YES" : "NO"));
        k = i*i*i*i;
        System.out.println("j= " +j+ ", k= " +k);

        //now multiplying with j instead of i
        System.out.println("\nNow Im working with a long type:");
        k = j*j*j;
        System.out.println("j= " +j+ ", k= " +k);           
        System.out.println("Is k less than Integer.MAX_VALUE: "  + (k < Integer.MAX_VALUE? "YES" : "NO"));
        k = j*j*j*j;
        System.out.println("j= " +j+ ", k= " +k);
        System.out.println("Is k less than Integer.MAX_VALUE: "  + (k < Integer.MAX_VALUE? "YES" : "NO"));
        k = j*j*j*j;

     }

}

The code above shows that when you have multiplied i 3 times the result is a value that is smaller than Integer.MAX_VALUE (which is 2147483647), and when youre multiplying it 4 times the result is 0 since there is no enough place in the poor 4 bytes of the multipliers. :) But you can see that when the multipliers (right side, j) are of type long, the correct value is printed, and the last printing shows that the statement k < Integer.MAX_VALUE is false, which is the reason for your zero. :)

上面的代码显示,当你将 i 乘以 3 倍时,结果是一个小于 Integer.MAX_VALUE(即 2147483647)的值,当你将它乘以 4 倍时,结果为 0,因为穷人没有足够的位置4 个字节的乘法器。:) 但是你可以看到,当乘数(右边,j)是long类型的时候,打印出正确的值,最后打印显示语句k < Integer.MAX_VALUE为false,这就是你的0的原因. :)