8/14/25, 9:51 PM Java Math
Tutorials Exercises Services Sign In
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Java Math
❮ Previous Next ❯
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:
Example Get your own Java Server
Math.max(5, 10);
Try it Yourself »
Math.min(x,y)
The Math.min(x,y) method can be used to find the lowest value of x and y:
Example
https://www.w3schools.com/java/java_math.asp 1/6
8/14/25, 9:51 PM Java Math
Math.min(5,
Tutorials 10);
Exercises Services Sign In
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Try it Yourself »
Math.sqrt(x)
The Math.sqrt(x) method returns the square root of x:
Example
Math.sqrt(64);
Try it Yourself »
Math.abs(x)
The Math.abs(x) method returns the absolute (positive) value of x:
Example
Math.abs(-4.7);
Try it Yourself »
Random Numbers
Math.random() returns a random number between 0.0 (inclusive), and 1.0 (exclusive):
https://www.w3schools.com/java/java_math.asp 2/6
8/14/25, 9:51 PM Java Math
Tutorials
Example Exercises Services Sign In
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
Math.random();
Try it Yourself »
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:
Example
int randomNum = (int)(Math.random() * 101); // 0 to 100
Try it Yourself »
Complete Math Reference
For a complete reference of Math methods, go to our Java Math Methods Reference.
?
Exercise
What is the output of the following code?
Math.min(5, 10);
15
10
50
https://www.w3schools.com/java/java_math.asp 3/6
8/14/25, 9:51 PM Java Math
Tutorials Exercises Services
Submit Answer »
Sign In
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
❮ Previous Next ❯
Track your progress - it's free! Sign Up Log in
COLOR PICKER
https://www.w3schools.com/java/java_math.asp 4/6
8/14/25, 9:51 PM Java Math
Tutorials Exercises Services Sign In
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
PLUS SPACES
GET CERTIFIED FOR TEACHERS
FOR BUSINESS CONTACT US
Top Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
How To Tutorial
SQL Tutorial
Python Tutorial
W3.CSS Tutorial
Bootstrap Tutorial
PHP Tutorial
Java Tutorial
C++ Tutorial
jQuery Tutorial
Top References
HTML Reference
CSS Reference
JavaScript Reference
SQL Reference
Python Reference
W3.CSS Reference
Bootstrap Reference
PHP Reference
HTML Colors
Java Reference
Angular Reference
jQuery Reference
Top Examples Get Certified
HTML Examples HTML Certificate
CSS Examples CSS Certificate
JavaScript Examples JavaScript Certificate
How To Examples Front End Certificate
SQL Examples SQL Certificate
Python Examples Python Certificate
W3.CSS Examples PHP Certificate
Bootstrap Examples jQuery Certificate
https://www.w3schools.com/java/java_math.asp 5/6
8/14/25, 9:51 PM Java Math
PHP Examples Java Certificate
Tutorials Java Examples
Exercises
XML Examples
Services C++ Certificate
C# Certificate
Sign In
jQuery Examples XML Certificate
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C
FORUM ABOUT ACADEMY
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and
learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full
correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie
and privacy policy.
Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.
https://www.w3schools.com/java/java_math.asp 6/6