# Absolute Value for MIN_VALUE in Java: Overflow Issue

## Overview

Absolute Value for MIN_VALUE in Java: a test code to get the absolute value for MIN_VALUE in Java is discussed and the reason why we get overflow is addressed.

## Absolute Value for MIN_VALUE in Java: Overflow Issue

The following Java code is interesting by its output at the first glimpse, the reason lies in the mechanism of two’s complement binary representation which I actually have already discussed in Chinese in this post: Why the absolute values are different for INT_MAX and INT_MIN in C++. This new post is a similar one but addressed the same issue in Java as in C++.

### The java testing code:

public class Test { public static void main(String [] args) { System.out.println("abs(MIN_VALUE) = " + Math.abs(Integer.MIN_VALUE)); System.out.println("-(MIN_VALUE) = " + -(Integer.MIN_VALUE)); } }

### The Java output:

abs(MIN_VALUE) = -2147483648 -(MIN_VALUE) = -2147483648

### Java Explain:

Integer.MIN_VALUE is -2147483648, but the highest value a 32 bit integer can contain is +2147483647. Attempting to represent +2147483648 in a 32 bit int will effectively “roll over” to -2147483648. This is because, when using signed integers, the two’s complement binary representations of +2147483648 and -2147483648 are identical. This is not a problem, however, as +2147483648 is considered out of range.

## Summary

Absolute Value for MIN_VALUE in Java: a test code to get the absolute value for MIN_VALUE in Java is discussed and the reason why we get overflow is addressed.