Java Math.abs() returns negative for Integer.MIN_VALUE

🤯 Most developers get this wrong! The misconception: You might think the output is 2147483648. After all, Math.abs() should return a positive value… right? The reality: The output is actually -2147483648. Yes — still negative. Why does this happen? 🧠 • Integer limits: In Java, int is a 32-bit signed integer. Integer.MIN_VALUE = -2147483648 • Maximum value: Integer.MAX_VALUE = 2147483647 • The catch (overflow): The positive counterpart of Integer.MIN_VALUE doesn’t fit in an int. Because Java uses two’s complement, it overflows… and wraps right back to the same negative value. How to handle it? 🛠️ Use Math.absExact() if you want to catch this edge case. It throws an ArithmeticException instead of silently overflowing. Drop a 💻 if you knew this, or a 🤯 if this surprised you! #Java #Programming #Coding #DeveloperLife #TechTrivia #ComputerScience #SoftwareEngineering #LearnToCode #CodingMisconceptions

  • graphical user interface, text

To view or add a comment, sign in

Explore content categories