Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

int to long assignment

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

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 i to long. i.e.

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 j and k. So, I wanted to know why is this needed to cast int in the second case but not in the first one. k, being a long can retain this value after this heavy assignment. But, why it is not been correctly assigned?

like image 431
Shashish Chandra Avatar asked Jul 15 '14 10:07

Shashish Chandra


People also ask

How to convert int to long in C++?

Since integer is a smaller data type compared to long, the compiler automatically converts int to long, this is known as Implicit type casting or type promotion. 2. Long.valueOf () method: In this, we are converting int to long using the valueOf () method of the Long Wrapper class.

How do you represent int values in a long?

A long can always represent all values of int . If the value at hand can be represented by the type of the variable you assign to, then the value is preserved.

Can I assign a long value to an unsigned int object?

(The rules are different for unsigned types; the result of converting a signed or unsigned integer to an unsigned type is well defined.) If you need to safely assign a long value to an int object, you can check that it will fit before doing the assignment:

How to convert an integer to a long in Java?

We can use the Java assignment operator (=) to convert lower data type (integer) to higher data type (long). The equals operator implicitly converts the integer to long. In the below Java example, we declared an integer variable and assigned 10.


2 Answers

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 kn overflows, you get the error.

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

Of course the simplest modification to your initial program is to define i as a long straight away, instead of an int.

long i = 1024L;
like image 174
dav_i Avatar answered Sep 20 '22 13:09

dav_i


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.

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. :)

like image 26
Ranic Avatar answered Sep 18 '22 13:09

Ranic