Java Math

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 11 / 50 · English

Java Math

Let the standard library do the calculation.

Step-by-step explanationsRunnable JavaPractice + answer

Read this topic in தமிழ் ↗.

By the end of this lesson, you can…

  • Explain the key idea behind Math.
  • Read and run a complete example, then predict what changes.
  • Recognize the topic-specific mistake discussed below.
01 / The concept

Understand the idea

Math groups common numeric operations. Read the return type as well as the method name: sqrt returns a double, while max has overloads for several numeric types. Floating-point calculations can be approximate, so test against appropriate tolerances when needed.

02 / Step by step

Explore the details

Java Math Class

The Math class provides ready-to-use methods for common mathematical operations — no imports needed, just call Math.methodName().

Key Math Methods

  • Math.max(x, y) — returns the larger value
  • Math.min(x, y) — returns the smaller value
  • Math.abs(x) — removes the sign, always returns positive
  • Math.sqrt(x) — square root of x
  • Math.pow(base, exp) — base raised to the power of exp
  • Math.round(x) — rounds to nearest integer
  • Math.ceil(x) — always rounds up
  • Math.floor(x) — always rounds down
  • Math.random() — random double between 0.0 and 1.0

Math.random() returns a different value every time the program runs. To get a random integer between 0 and N, use (int)(Math.random() * N).

Java reference fragmentJAVA
System.out.println(Math.max(15, 42));   // 42
System.out.println(Math.min(15, 42));   // 15
System.out.println(Math.abs(-64));      // 64
System.out.println(Math.sqrt(144));     // 12.0
System.out.println(Math.pow(2, 10));    // 1024.0
System.out.println(Math.round(4.6));    // 5
System.out.println(Math.ceil(4.2));     // 5.0
System.out.println(Math.floor(4.9));    // 4.0

// Random integer 0–99
int rand = (int)(Math.random() * 100);
System.out.println(rand);  // e.g. 73 (changes every run)
03 / Read and predict

A complete runnable example

This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.

Main.java · complete runnable exampleJAVA
public class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(Math.max(12, 27));
        System.out.println(Math.abs(-8));
        System.out.println(Math.sqrt(81));
        System.out.println(Math.round(4.6));
    }
}
Predict first. Then reveal the expected output / வெளியீடு
27
8
9.0
5

The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.

04 / Learn by changing

Change it. Run it. Understand it.

Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.

05 / Your turn

Make it your own

Practice challenge

Change the square-root argument to 2 and compare the type and value of the output.

Reveal the answer and reasoning / விடை

Math.sqrt(2) produces an approximate double value, about 1.4142135623730951.

One mistake to avoid: Math.pow returns a double. The ^ operator is bitwise XOR for integer operands, not exponentiation.

Your learning checkpoint

  • Explain the example without reading the output first.
  • Make the change requested in the practice task.
  • Describe why the result changed—or why compilation failed.

Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.

Coding Tamilan · Understand the idea. Try the code. Build the habit.