-2

I’m recently facing the below problem

 public class Main {
    public static void main(String[] args) throws Exception {
        // Your code here!
        int i = -2147483648;
        int j = i * -1;
        
        System.out.println("j="+j);
    }
}

Result : -2147483648

Online IDE with code : https://paiza.io/projects/e/17lF_6-GltIcyubZv3QoFg?theme=twilight

But how it’s works as per the logic I need to get 2147483648 is a result right? Then how I got this negative number ? It’s because of integer range (Integer.MIN_VALUE)? how to fix this issue?

  • 1
    The number is to large for an int. Use long instead. – Eritrean Dec 15 '20 at 19:53
  • the fix is easy, use long. The explanation is in figuring out 2s complement and the representation and how to detect an overflow. See Math.exact in Java > 8 – Khanna111 Dec 15 '20 at 20:21

2 Answers2

1

The number 2147483648 does not exists. The biggest value of an int is 2147483647, which is 1 smaller than your expected result. The multiplication causes an overflow, which 'rolls back' the number to the smallest negative value, and continues the calculation from there. (With other words: 2147483647+1=-2147483648 (smallest negative)) Since the result would only be 1 over the maximum value, there is no additional action required and the minimal int value is returned.

If you want to fix this issue, use 'long' instead of 'int' for your variables. You can also use more complex classes like BigDecimal, or write a custom multiplication function for byte arrays.

Note: no matter what numeric type you use, as long as the memory used for representing the number is finite you can run into similar issues. Although under normal circumstances it is unlikely, even for a 32-bit integer (int).

  • The number exists. It just can't be held in a signed int. – NomadMaker Dec 15 '20 at 20:10
  • I meant it doesn't exist for the specified type. Java doesn't support unsigned values, and even then the concept of multiplication with a negative value would be interesting. –  Dec 15 '20 at 20:11
1

The maximum postive value an int can hold is 2147483647 beyond which the value goes to the other end (i.e. it starts from the negative end). You can understand it from the following demo:

public class Main {
    public static void main(String[] args) {
        int i = -2147483648;
        int j = i * -1;

        System.out.println("j=" + j);

        System.out.println(Integer.MAX_VALUE);
        System.out.println(Integer.MAX_VALUE + 1);
        System.out.println(Integer.MIN_VALUE);

        // Some more examples for you to understand this concept better
        System.out.println(Integer.MAX_VALUE + 2);
        System.out.println(Integer.MAX_VALUE + 3);
    }
}

Output:

j=-2147483648
2147483647
-2147483648
-2147483648
-2147483647
-2147483646

After -2147483648 is multipled with -1, it becomes 2147483648 but an int variable can not hold this much big positive value; so, it will start from the negative end (i.e. Integer.MIN_VALUE).

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110