Java Math
The Java Math class has many methods that allows you to perform mathematical tasks on numbers.
Math.max(x,y)
The Math.max(x,y)
method can be used to find the highest value of x and
y:
Math.min(x,y)
The Math.min(x,y)
method can be used to find the lowest value of x
and y:
Math.sqrt(x)
The Math.sqrt(x)
method returns the square root of x:
Math.abs(x)
The Math.abs(x)
method returns the absolute (positive) value of x:
Random Numbers
Math.random()
returns a random number between 0.0 (inclusive), and 1.0 (exclusive):
To get more control over the random number, for example, if you only want a random number between 0 and 100, you can use the following formula:
Complete Math Reference
For a complete reference of Math methods, go to our Java Math Methods Reference.
Exercise?What is this?
Test your skills by answering a few questions about the topics of this page
What is the output of the following code?Math.min(5, 10);