• Martin Thoma
  • Home
  • Categories
  • Tags
  • Archives
  • Support me

Java Puzzle #13: Absolute Value Weirdness

Contents

  • Explanation
    • Modern Solutions

What does the following Java snippet output?

public class SomeClass {
    public static void main(String[] args) {
        int a = -10;
        int b = -2147483648; // -2147483648 == -2**31

        if (Math.abs(a) < -1) {
            System.out.println("|a| < -1");
        } else {
            System.out.println("|a| >= -1");
        }

        if (Math.abs(b) < -1) {
            System.out.println("|b| < -1");
        } else {
            System.out.println("|b| >= -1");
        }

        System.out.println("|a| = " + Math.abs(a));
        System.out.println("|b| = " + Math.abs(b));
    }
}
Click to see the Answer
|a| >= -1
|b| < -1
|a| = 10
|b| = -2147483648

Explanation

Integer values in Java range from -2,147,483,648 to 2,147,483,647 (32-bit signed integer). This means the absolute value of -2,147,483,648 cannot be represented as a positive integer in the same data type.

When Math.abs(-2147483648) is called, it actually returns -2,147,483,648 because:

  1. The mathematical absolute value would be 2,147,483,648
  2. But this value exceeds the maximum positive integer (2,147,483,647)
  3. Due to integer overflow, it wraps around to the minimum negative value

This is why |b| < -1 evaluates to true - because Math.abs(b) returns a negative number!

Modern Solutions

In modern Java, you can use: - Math.absExact() - throws an exception on overflow - long data type for larger ranges - BigInteger for arbitrary precision

For more details, see this Stack Overflow answer.

Published

Okt 19, 2012
by Martin Thoma

Category

Code

Tags

  • Java 36
  • Programming 52
  • puzzle 22

Contact

  • Martin Thoma - A blog about Code, the Web and Cyberculture
  • E-mail subscription
  • RSS-Feed
  • Privacy/Datenschutzerklärung
  • Impressum
  • Powered by Pelican. Theme: Elegant by Talha Mansoor