Java Math
Let the standard library do the calculation.
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.
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.
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 valueMath.min(x, y)— returns the smaller valueMath.abs(x)— removes the sign, always returns positiveMath.sqrt(x)— square root of xMath.pow(base, exp)— base raised to the power of expMath.round(x)— rounds to nearest integerMath.ceil(x)— always rounds upMath.floor(x)— always rounds downMath.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).
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)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.
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.
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.
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.
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.