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
- Start with −3
- Do this Measure distance from zero
- Result 3
class Main {
public static void main(String[] args) throws Exception {
System.out.println(Math.abs(-3));
}
}
3
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.javaHow 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.
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));
}
}
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.
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.
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));
}
}
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.
class Main {
public static void main(String[] args) throws Exception {
int scanned = 47;
int expected = 52;
System.out.println(Math.abs(scanned - expected));
}
}
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.
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");
}
}
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.
The two readings sit seven one-degree intervals apart on the same scale.
Code and output in Java
class Main {
public static void main(String[] args) throws Exception {
int sunrise = -3, midday = 4;
System.out.println(Math.abs(midday - sunrise) + " °C");
}
}
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.