0Pricing
Java Academy · Lesson

The Math Class

Common math functions.

The Math Class is a free Java Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Math Class

java.lang.Math is a built-in class of static math methods. It is in java.lang, so no import is needed. You call methods on the class: Math.sqrt(2).

Powers and Roots

Math.pow(base, exp) raises a number to a power, and Math.sqrt(x) takes a square root. Both return double.

public class Main {
    public static void main(String[] args) {
        System.out.println(Math.pow(2, 10));
        System.out.println(Math.sqrt(144));
    }
}

Absolute Value

Math.abs returns the magnitude of a number, dropping the sign. It is overloaded for int, long, float, and double.

public class Main {
    public static void main(String[] args) {
        System.out.println(Math.abs(-42));
        System.out.println(Math.abs(-3.14));
    }
}

Min and Max

Math.min and Math.max return the smaller or larger of two values.

public class Main {
    public static void main(String[] args) {
        System.out.println(Math.max(10, 25));
        System.out.println(Math.min(10, 25));
    }
}

Constants PI and E

Math exposes two famous constants: Math.PI and Math.E. They are public static final double fields.

public class Main {
    public static void main(String[] args) {
        double radius = 3;
        double area = Math.PI * radius * radius;
        System.out.println("Area: " + area);
    }
}

Trigonometry

Trig functions like Math.sin, Math.cos, and Math.tan work in radians, not degrees. Convert with Math.toRadians.

public class Main {
    public static void main(String[] args) {
        double degrees = 90;
        double radians = Math.toRadians(degrees);
        System.out.println(Math.sin(radians));
    }
}

Logarithms and Exponentials

Math.log is the natural log (base e), Math.log10 is base 10, and Math.exp raises e to a power.

public class Main {
    public static void main(String[] args) {
        System.out.println(Math.log10(1000));
        System.out.println(Math.exp(1));
    }
}

Hypot for Distance

Math.hypot(x, y) computes the square root of x squared plus y squared without intermediate overflow, ideal for 2D distances.

public class Main {
    public static void main(String[] args) {
        double dist = Math.hypot(3, 4);
        System.out.println(dist);
    }
}

Integer Overflow Helpers

Methods like Math.addExact and Math.multiplyExact throw an ArithmeticException if the result overflows, instead of silently wrapping around.

public class Main {
    public static void main(String[] args) {
        try {
            int result = Math.addExact(Integer.MAX_VALUE, 1);
            System.out.println(result);
        } catch (ArithmeticException e) {
            System.out.println("Overflow detected: " + e.getMessage());
        }
    }
}

Return Types Are Double

Most Math methods return double. If you need an int, you must cast explicitly: (int) Math.sqrt(16). Be aware casting truncates toward zero.

public class Main {
    public static void main(String[] args) {
        int side = (int) Math.sqrt(16);
        System.out.println(side);
    }
}

A Mini Calculation

Combine several Math methods to compute a result, such as the hypotenuse via squares and a square root.

public class Main {
    public static void main(String[] args) {
        double a = 5, b = 12;
        double c = Math.sqrt(Math.pow(a, 2) + Math.pow(b, 2));
        System.out.println("Hypotenuse: " + c);
    }
}

Quick Check

Test your understanding of the Math class.

Recap

You explored the Math class.

  • It is static and in java.lang, so no import.
  • pow, sqrt, abs, min, max cover common operations.
  • Constants PI and E are available.
  • Trig uses radians; most methods return double.

Frequently asked questions

Is the “The Math Class” lesson free?

Yes — the full text of “The Math Class” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “The Math Class”?

Common math functions. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “The Math Class” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. The Math Class
  2. Generating Random Numbers
  3. Random Ranges and Shuffling
  4. Math Rounding and Limits
← Back to Java Academy