Java Tutorial

Java Math Class: Methods with Examples (abs, pow, sqrt, round, random)

TGTeam GyaniPanditNovember 20, 20264 min read

Java’s Math class (in the java.lang package) gives you ready-made methods for common mathematical work: absolute values, powers, square roots, rounding, trigonometry, logarithms and random numbers. You don’t need to import it or create an object; every method is static, so you call it directly as Math.methodName().

Commonly Used Math Methods

Method What it returns Example Result
Math.abs(x) Absolute (positive) value Math.abs(-7.5) 7.5
Math.max(a, b) Larger of two values Math.max(10, 20) 20
Math.min(a, b) Smaller of two values Math.min(10, 20) 10
Math.sqrt(x) Square root Math.sqrt(64) 8.0
Math.cbrt(x) Cube root Math.cbrt(27) 3.0
Math.pow(a, b) a raised to the power b Math.pow(2, 10) 1024.0
Math.hypot(x, y) √(x² + y²) Math.hypot(3, 4) 5.0
Math.ceil(x) Smallest whole number ≥ x Math.ceil(4.2) 5.0
Math.floor(x) Largest whole number ≤ x Math.floor(4.8) 4.0
Math.round(x) Nearest whole number (as long/int) Math.round(4.5) 5
Math.random() Random double from 0.0 up to (not including) 1.0 Math.random() e.g. 0.7314...
Math.log(x), Math.log10(x) Natural log, log base 10 Math.log10(1000) 3.0
Math.signum(x) -1.0, 0.0 or 1.0 depending on the sign Math.signum(-3.2) -1.0

Notice that most methods return a double, which Java prints with .0, even when the answer is a whole number.

Example Program

public class MathDemo {
    public static void main(String[] args) {
        System.out.println(Math.abs(-7.5));
        System.out.println(Math.max(10, 20));
        System.out.println(Math.sqrt(64));
        System.out.println(Math.pow(2, 10));
        System.out.println(Math.ceil(4.2));
        System.out.println(Math.floor(4.8));
        System.out.println(Math.round(4.5));
        System.out.println(Math.round(-4.5));
    }
}

Output:

7.5
20
8.0
1024.0
5.0
4.0
5
-4

Math.round() rounds halves up (towards positive infinity), so -4.5 becomes -4, not -5.

Constants: Math.PI and Math.E

System.out.println(Math.PI);
System.out.println(Math.E);

double radius = 7;
double area = Math.PI * radius * radius;
System.out.println(area);

Output:

3.141592653589793
2.718281828459045
153.93804002589985

Trigonometry Uses Radians

Java’s sin(), cos() and tan() expect radians, not degrees. Convert with Math.toRadians():

System.out.println(Math.toRadians(180));
System.out.println(Math.sin(Math.toRadians(30)));
System.out.println(Math.cos(Math.toRadians(60)));

Output:

3.141592653589793
0.49999999999999994
0.5000000000000001

You might expect exactly 0.5. The tiny difference comes from how computers store decimal numbers (floating point). Round the result if you need to display it.

Practical Example: Compound Interest

double principal = 10000;   // ₹10,000
double rate = 8;            // 8% per year
int years = 3;

double amount = principal * Math.pow(1 + rate / 100, years);
System.out.println(Math.round(amount * 100) / 100.0);

Output:

12597.12

Multiplying by 100, rounding and dividing by 100.0 is a common way to keep two decimal places. (For real money calculations, Java programs usually use BigDecimal.)

Random Numbers in a Range

// random dice roll from 1 to 6
int dice = (int) (Math.random() * 6) + 1;
System.out.println(dice);

Math.random() gives a number from 0.0 to just under 1.0. Multiplying by 6 gives 0.0 to just under 6.0; casting to int gives 0 to 5; adding 1 gives 1 to 6. For many random numbers, the java.util.Random class is more convenient.

Integer Division Traps

System.out.println(-7 / 2);                 // integer division truncates
System.out.println(Math.floorDiv(-7, 2));   // rounds down
System.out.println(Math.floorMod(-7, 2));   // always non-negative for a positive divisor
System.out.println(Math.floor(10 / 3));     // 10 / 3 is already the int 3

Output:

-3
-4
1
3.0

In the last line, 10 / 3 is integer division, giving 3 before floor() even runs. Write 10 / 3.0 if you want decimal division.

Frequently Asked Questions

Do I need to import the Math class in Java?

No. It is in java.lang, which Java imports automatically.

Why does Math.sqrt(64) print 8.0 and not 8?

Because sqrt() returns a double. Cast it with (int) Math.sqrt(64) if you need an integer.

What is the difference between Math.round(), Math.ceil() and Math.floor()?

round() goes to the nearest whole number, ceil() always goes up, and floor() always goes down. See Java Math.floor().

Related: Java methods · Assignment operators in Java · Arithmetic operators in Java

Turn reading into skillsCertificate courses in AI, Data Science and programming.
Explore Courses