Library contents

Java Math.abs

Math.abs gives the size of a difference. A fall from 22 degrees to 16 degrees is a change of 6 degrees. Use the original difference when the direction matters.

A simple example

Math.abs

  1. Start with −3
  2. Do this Measure distance from zero
  3. Result 3
From −3, three unit steps reach zero. The magnitude is 3.
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(Math.abs(-3));
    }
}
Output
3

Syntax

Java syntax
Math.abs(value)
JDK 21 method signatures (4)
public static int abs(int a)
public static long abs(long a)
public static float abs(float a)
public static double abs(double a)
Run these examples in Java

Save one example at a time as Main.java. Use JDK 21.

java Main.java

How far did the temperature move?

A student records temperatures at two times of day. The signed difference says which reading was warmer, while the magnitude answers how far apart the two readings were.

Given22 °C in the morning → 16 °C in the evening
First output lineChange: -6
Main.java
public class Main {
    public static void main(String[] args) throws Exception {
        int morning = 22;
        int evening = 16;
        int change = evening - morning;
        System.out.println("Change: " + change);
        System.out.println("Degrees apart: " + Math.abs(change));
    }
}
Output
Change: -6
Degrees apart: 6

Subtracting morning from evening gives −6. The sign tells you it cooled down. Taking the absolute value gives the distance between the readings.

Advertisements

Compare two extreme meter readings

Two signed readings can be individually valid while their difference or magnitude exceeds the destination type. Check the arithmetic at its boundaries before reporting the size of the change.

First output line4294967295
Main.java
public class Main {
    public static void main(String[] args) throws Exception {
        int low = Integer.MIN_VALUE;
        int high = Integer.MAX_VALUE;
        long gap = Math.abs((long) high - low);
        System.out.println(gap);
        System.out.println(Math.abs(low));
    }
}
Output
4294967295
-2147483648

Casting high before the subtraction keeps the entire difference in a long. The last line demonstrates the special int case. Math.absExact(low) would throw ArithmeticException.

Reconcile a stocktake

A stocktake compares what the scanner found with what the inventory system expected. The report needs to distinguish a shortage, a surplus and an exact match before deciding whether to investigate.

Given52 on the manifest · 47 scanned
Result5
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        int scanned = 47;
        int expected = 52;
        System.out.println(Math.abs(scanned - expected));
    }
}
Output
5

The scanner found 47 items against 52 on the manifest. The discrepancy is five items. Keep the signed difference separately to distinguish a shortage from a surplus.

Reconcile a stockroom count

A school library expects 48 loan chargers on its shelf after class. A student counts 45. The handover form has a separate field for the size of the discrepancy, whether the shelf is short or over.

Given48 chargers on record · 45 on the shelf
Result3 chargers
Main.java
class Main {
    public static void main(String[] args) throws Exception {
        int expected = 48;
        int counted = 45;
        System.out.println(Integer.toString(Math.abs(counted - expected)) + " chargers");
    }
}
Output
3 chargers

The signed difference is minus three. Its magnitude gives the three chargers the next shift needs to account for.

Compare two morning readings

A class logs the courtyard temperature at sunrise and again before lunch. The report asks how far apart the readings are, even when one is below freezing.

Given−3 °C → 4 °C
Worked result7 °C

The two readings sit seven one-degree intervals apart on the same scale.

Code and output in Java
Java source
class Main {
    public static void main(String[] args) throws Exception {
        int sunrise = -3, midday = 4;
        System.out.println(Math.abs(midday - sunrise) + " °C");
    }
}
Output
7 °C

What to watch for

For int and long, the smallest value has no positive counterpart in the same type. Widen an int to long before subtraction when the difference may exceed the int range.

Documentation for Math.abs