Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

java.lang.Math — absolute value and minimum

Math provides static numeric operations. Learn Math.abs and Math.min, including their behavior for extreme integers, NaN, and signed zero.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

Math.abs(a)
Math.min(a, b)

java.lang.Math provides numeric operations as static methods: call them through the class name without creating an object. The java.lang package is imported automatically, so no import statement is needed.

Math.abs(a) computes an absolute value, while Math.min(a, b) returns the smaller of two numbers. Both methods have overloads for int, long, float, and double.

Warning: Math.abs(Integer.MIN_VALUE) and Math.abs(Long.MIN_VALUE) return the same negative argument because of overflow, without throwing an exception. For float and double, Math.abs turns negative zero into positive zero and returns NaN for NaN. Math.min returns NaN if either argument is NaN and chooses negative zero over positive zero.

Examples

public class Main {
    public static void main(String[] args) {
        System.out.println(Math.abs(-12));
        System.out.println(Math.min(12, 8));
        System.out.println(Math.abs(Integer.MIN_VALUE));
        System.out.println(Math.min(0.0, -0.0));
    }
}

Result

12
8
-2147483648
-0.0

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources