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 Essentials 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

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

Power, Square Root, and Cube Root

Use Math.pow(base, exp) for powers, Math.sqrt() for square roots, and Math.cbrt() for cube roots.

System.out.println(Math.pow(2, 10));  // 1024.0
System.out.println(Math.sqrt(144));   // 12.0
System.out.println(Math.cbrt(27));    // 3.0

// Hypotenuse of a right triangle (a=3, b=4)
double hyp = Math.sqrt(Math.pow(3, 2) + Math.pow(4, 2));
System.out.println(hyp); // 5.0

Min, Max, and Clamp

Math.min() and Math.max() return the smaller/larger of two values. Combine them to implement a clamp that keeps a value within a range.

int a = 10, b = 20;
System.out.println(Math.min(a, b)); // 10
System.out.println(Math.max(a, b)); // 20

// Clamp value between 0 and 100
int clamp(int value, int min, int max) {
    return Math.max(min, Math.min(value, max));
}
System.out.println(clamp(150, 0, 100)); // 100
System.out.println(clamp(-5,  0, 100)); // 0
System.out.println(clamp(75,  0, 100)); // 75

Floor, Ceiling, and Round

Math.floor() rounds down, Math.ceil() rounds up, and Math.round() rounds to the nearest integer (half-up).

double price = 9.3;
System.out.println(Math.floor(price));  // 9.0
System.out.println(Math.ceil(price));   // 10.0
System.out.println(Math.round(price));  // 9

double shipping = 3.5;
System.out.println(Math.round(shipping)); // 4 (half-up)

// Round to 2 decimal places
double tax = 7.5678;
double rounded = Math.round(tax * 100.0) / 100.0;
System.out.println(rounded); // 7.57

Logarithms and Exponentials

Math.log() computes natural log (base e), Math.log10() computes base-10 log, and Math.exp() computes e^x.

System.out.println(Math.log(Math.E));   // 1.0
System.out.println(Math.log10(1000));   // 3.0
System.out.println(Math.exp(1));        // 2.718281828...

// Change of base: log2(8)
double log2 = Math.log(8) / Math.log(2);
System.out.println(log2); // 3.0

// Compound interest: P * e^(r*t)
double principal = 1000, rate = 0.05, years = 10;
double amount = principal * Math.exp(rate * years);
System.out.printf("Amount: %.2f%n", amount); // 1648.72

Trigonometric Methods

Math provides trig functions that accept angles in radians. Use Math.toRadians() to convert degrees to radians.

double angle = Math.toRadians(45);
System.out.printf("sin(45): %.4f%n", Math.sin(angle)); // 0.7071
System.out.printf("cos(45): %.4f%n", Math.cos(angle)); // 0.7071
System.out.printf("tan(45): %.4f%n", Math.tan(angle)); // 1.0000

// Distance between two GPS coordinates (simplified)
double lat1 = Math.toRadians(40.7128);
double lat2 = Math.toRadians(34.0522);
double deltaLat = lat2 - lat1;
System.out.printf("Delta lat (rad): %.4f%n", deltaLat);

Random Numbers

Math.random() returns a double in [0.0, 1.0). Multiply and cast to get integers in a range. For better randomness, prefer java.util.Random.

// Random double [0.0, 1.0)
double rand = Math.random();

// Random int [min, max] inclusive
int min = 1, max = 6;
int dice = (int) (Math.random() * (max - min + 1)) + min;
System.out.println("Dice roll: " + dice); // 1–6

// Prefer java.util.Random for more features
import java.util.Random;
Random rng = new Random();
int secureRoll = rng.nextInt(6) + 1; // cleaner API
System.out.println("Secure roll: " + secureRoll);

Constants: PI and E

Math.PI and Math.E are double constants for π and Euler's number e. Use them for geometric and financial calculations.

System.out.println(Math.PI); // 3.141592653589793
System.out.println(Math.E);  // 2.718281828459045

// Circle area and circumference
double radius = 7.0;
double area = Math.PI * Math.pow(radius, 2);
double circumference = 2 * Math.PI * radius;

System.out.printf("Area: %.2f%n", area);           // 153.94
System.out.printf("Circumference: %.2f%n", circumference); // 43.98

Math.floorDiv and Math.floorMod

Math.floorDiv() and Math.floorMod() handle negative numbers consistently — unlike the / and % operators which can give surprising results with negatives.

// Regular % can be negative with negative dividend
System.out.println(-7 % 3);           // -1 (may surprise you)
System.out.println(Math.floorMod(-7, 3)); // 2  (always non-negative)

// Floor division rounds toward negative infinity
System.out.println(-7 / 3);              // -2 (truncates toward zero)
System.out.println(Math.floorDiv(-7, 3)); // -3 (floors down)

// Use floorMod for cyclic calculations like days of week
int day = Math.floorMod(-1, 7); // Sunday (6), not -1
System.out.println(day); // 6

Practical: Distance and Physics

Applying Math methods to a physics problem: calculating projectile range using trigonometry and square roots.

// Projectile range: R = v^2 * sin(2*angle) / g
double velocity = 50.0;   // m/s
double angleDeg = 45.0;   // degrees
double g = 9.81;          // m/s^2

double angleRad = Math.toRadians(angleDeg);
double range = Math.pow(velocity, 2) * Math.sin(2 * angleRad) / g;

System.out.printf("Projectile range: %.1f m%n", range); // 254.8 m

Quick Check

What is the output of Math.floorMod(-7, 3)?

Recap: The Math Class

Key takeaways:

  • Math.abs, Math.min, Math.max for basic comparisons
  • Math.pow, Math.sqrt, Math.cbrt for exponents and roots
  • Math.floor, Math.ceil, Math.round for rounding
  • Math.log, Math.log10, Math.exp for logarithms/exponentials
  • Math.sin, Math.cos, Math.tan use radians — convert with Math.toRadians
  • Math.floorMod handles negative numbers cleanly for cyclic arithmetic
  • Math.PI and Math.E are built-in constants

Frequently asked questions

Is the “The Math Class Essentials” lesson free?

Yes — the full text of “The Math Class Essentials” 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 Essentials”?

Use Math.abs, Math.pow, Math.sqrt, Math.random, and other frequently used Math methods. 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 Essentials” 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 Essentials
  2. Integer Arithmetic & Overflow
  3. BigDecimal for Financial Calculations
  4. NumberFormat and printf
← Back to Java Academy