Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

Java Math

Java Math

The Java Math class offers a variety of methods enabling you to execute mathematical operations and calculations with numbers.

Math.max(x,y)

You can utilize the Math.max(x, y) method to determine the greater value between x and y.

Example

Math.max(5, 10);

Math.min(x,y)

The Math.min(x, y) method facilitates identifying the smaller value between x and y:

Example

Math.min(5, 10);

Math.sqrt(x)

The Math.sqrt(x) method provides the square root value of x.

Example

Math.sqrt(64);

Math.abs(x)

The Math.abs(x) method yields the absolute, or positive, value of x.

Example

Math.abs(-4.7);

Random Numbers

Math.random() generates a random number within the range of 0.0 (inclusive) to 1.0 (exclusive).

Example

Math.random();

For finer control over random number generation, such as limiting it to a range like 0 to 100, you can employ the following formula:

Example

int randomNum = (int)(Math.random() * 101);  // 0 to 100