Skip to main content

Command Palette

Search for a command to run...

Java Math Methods in Java

Author by Manikandan S - 26th September 2026

Updated
•4 min read•View as Markdown
Java Math Methods in Java
M
Product Engineer with 3.4 years of experience in Java, Spring Boot, React, Vue, SQL, AWS, and Warehouse Management Systems. Passionate about backend development and software engineering.

Introduction

The Math class in Java provides built-in methods for performing common mathematical operations.

It belongs to the:

java.lang.Math

No import is required.


1. Math.abs()

Returns the absolute value of a number.

Syntax

Math.abs(number);

Example

int result = Math.abs(-10);

System.out.println(result);

Output:

10

2. Math.max()

Returns the larger of two numbers.

int result = Math.max(10, 20);

System.out.println(result);

Output:

20

3. Math.min()

Returns the smaller of two numbers.

int result = Math.min(10, 20);

System.out.println(result);

Output:

10

4. Math.pow()

Returns a number raised to a power.

Syntax

Math.pow(base, exponent);

Example

double result = Math.pow(2, 3);

System.out.println(result);

Output:

8.0

5. Math.sqrt()

Returns the square root of a number.

double result = Math.sqrt(25);

System.out.println(result);

Output:

5.0

6. Math.cbrt()

Returns the cube root of a number.

double result = Math.cbrt(27);

System.out.println(result);

Output:

3.0

7. Math.round()

Rounds a number to the nearest integer.

long result = Math.round(10.6);

System.out.println(result);

Output:

11

8. Math.ceil()

Rounds a number upward.

double result = Math.ceil(10.2);

System.out.println(result);

Output:

11.0

9. Math.floor()

Rounds a number downward.

double result = Math.floor(10.8);

System.out.println(result);

Output:

10.0

10. Math.random()

Returns a random double value between 0.0 and less than 1.0.

double result = Math.random();

System.out.println(result);

Example output:

0.583421

Generate a number from 1 to 100

int number = (int) (Math.random() * 100) + 1;

System.out.println(number);

11. Math.signum()

Returns the sign of a number.

System.out.println(Math.signum(10));
System.out.println(Math.signum(-10));
System.out.println(Math.signum(0));

Output:

1.0
-1.0
0.0

12. Math.negateExact()

Returns the negation of an integer or long value.

int result = Math.negateExact(10);

System.out.println(result);

Output:

-10

13. Math.addExact()

Adds two numbers and throws an exception if integer overflow occurs.

int result = Math.addExact(10, 20);

System.out.println(result);

Output:

30

14. Math.subtractExact()

Subtracts two numbers.

int result = Math.subtractExact(20, 10);

System.out.println(result);

Output:

10

15. Math.multiplyExact()

Multiplies two numbers.

int result = Math.multiplyExact(10, 5);

System.out.println(result);

Output:

50

16. Math.incrementExact()

Increases a number by 1.

int result = Math.incrementExact(10);

System.out.println(result);

Output:

11

17. Math.decrementExact()

Decreases a number by 1.

int result = Math.decrementExact(10);

System.out.println(result);

Output:

9

18. Math.toIntExact()

Converts a long value to int.

long number = 100;

int result = Math.toIntExact(number);

System.out.println(result);

19. Mathematical Constants

Java provides commonly used mathematical constants.

Math.PI

System.out.println(Math.PI);

Example:

3.141592653589793

Math.E

System.out.println(Math.E);

Example:

2.718281828459045

20. Trigonometric Methods

Java provides common trigonometric methods.

Math.sin(x);
Math.cos(x);
Math.tan(x);

Example:

double result = Math.sin(Math.PI / 2);

System.out.println(result);

Output:

1.0

21. Logarithmic Methods

Common logarithmic methods:

Math.log(x);
Math.log10(x);

Example:

System.out.println(Math.log(10));
System.out.println(Math.log10(100));

Output:

2.302585...
2.0

22. Exponential Method

Math.exp() returns e raised to the specified power.

double result = Math.exp(2);

System.out.println(result);

23. Common Math Methods Summary

Method Purpose
abs() Absolute value
max() Maximum value
min() Minimum value
pow() Power
sqrt() Square root
cbrt() Cube root
round() Round value
ceil() Round upward
floor() Round downward
random() Random number
signum() Number sign
addExact() Exact addition
subtractExact() Exact subtraction
multiplyExact() Exact multiplication
incrementExact() Add 1
decrementExact() Subtract 1
toIntExact() Convert long to int
sin() Sine
cos() Cosine
tan() Tangent
log() Natural logarithm
log10() Base-10 logarithm
exp() Exponential value

Conclusion

The Java Math class provides ready-to-use methods for mathematical calculations, rounding, random numbers, powers, roots, trigonometry, and logarithms.

The most commonly used methods are:

  • abs()

  • max()

  • min()

  • pow()

  • sqrt()

  • round()

  • ceil()

  • floor()

  • random()

These methods are useful in Java applications, calculations, algorithms, and backend development.