0Pricing
Java Academy · Lesson

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.0

All lessons in this course

  1. The Math Class Essentials
  2. Integer Arithmetic & Overflow
  3. BigDecimal for Financial Calculations
  4. NumberFormat and printf
← Back to Java Academy