The Math Class Essentials
Use Math.abs, Math.pow, Math.sqrt, Math.random, and other frequently used Math methods.
The Math Class
The java.lang.Math class provides static utility methods for common mathematical operations. It's always available — no import needed.
Absolute Value and Sign
Math.abs() returns the absolute value (non-negative). Math.signum() returns -1.0, 0.0, or 1.0 depending on sign.
System.out.println(Math.abs(-42)); // 42
System.out.println(Math.abs(3.14)); // 3.14
System.out.println(Math.signum(-5.0)); // -1.0
System.out.println(Math.signum(0.0)); // 0.0
System.out.println(Math.signum(7.0)); // 1.0All lessons in this course
- The Math Class Essentials
- Integer Arithmetic & Overflow
- BigDecimal for Financial Calculations
- NumberFormat and printf