🚀 TechFlow Now

← Back to Resources

Java Math Methods

abs()

Returns the absolute value of a number.

System.out.println(Math.abs(-5)); // 5

max()

Returns the maximum of two numbers.

System.out.println(Math.max(10, 20)); // 20

min()

Returns the minimum of two numbers.

System.out.println(Math.min(10, 20)); // 10

sqrt()

Returns the square root of a number.

System.out.println(Math.sqrt(25)); // 5.0

pow()

Returns the first number raised to the power of the second.

System.out.println(Math.pow(2, 3)); // 8.0

ceil()

Returns the smallest integer greater than or equal to the number.

System.out.println(Math.ceil(5.3)); // 6.0

floor()

Returns the largest integer less than or equal to the number.

System.out.println(Math.floor(5.7)); // 5.0

round()

Rounds the value to nearest integer.

System.out.println(Math.round(5.5)); // 6

random()

Returns a random double between 0.0 and 1.0.

System.out.println(Math.random()); // e.g., 0.4832