Tutorials References Exercises Bootcamps Videos Menu
Sign Up Create Website Get Certified Upgrade

Java Tutorial

Java HOME Java Intro Java Get Started Java Syntax Java Output Java Comments Java Variables Java Data Types Java Type Casting Java Operators Java Strings Java Math Java Booleans Java If...Else Java Switch Java While Loop Java For Loop Java Break/Continue Java Arrays

Java Methods

Java Methods Java Method Parameters Java Method Overloading Java Scope Java Recursion

Java Classes

Java OOP Java Classes/Objects Java Class Attributes Java Class Methods Java Constructors Java Modifiers Java Encapsulation Java Packages / API Java Inheritance Java Polymorphism Java Inner Classes Java Abstraction Java Interface Java Enums Java User Input Java Date Java ArrayList Java LinkedList Java HashMap Java HashSet Java Iterator Java Wrapper Classes Java Exceptions Java RegEx Java Threads Java Lambda

Java File Handling

Java Files Java Create/Write Files Java Read Files Java Delete Files

Java How To

Add Two Numbers Count Words Reverse a String Sum of Array Elements Area of Rectangle Even or Odd Number

Java Reference

Java Keywords Java String Methods Java Math Methods

Java Examples

Java Examples Java Compiler Java Exercises Java Quiz Java Certificate

Java Math

The Java Math class has many methods that allows you to perform mathematical tasks on numbers.


The Math.max(x,y) method can be used to find the highest value of x and y:


Math.max(5, 10);

Try it Yourself »


The Math.min(x,y) method can be used to find the lowest value of x and y:


Math.min(5, 10);

Try it Yourself »


The Math.sqrt(x) method returns the square root of x:



Try it Yourself »


The Math.abs(x) method returns the absolute (positive) value of x:



Try it Yourself »

Random Numbers

Math.random() returns a random number between 0.0 (inclusive), and 1.0 (exclusive):



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:


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.

Test Yourself With Exercises


Use the correct method to find the highest value of x and y.

int x = 5;
int y = 10;
Math.(x, y);

Start the Exercise