🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Java Notes
Topic #33

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:

Example

Math.max(5, 10);

Math.min( x,y )

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

Example

Math.min(5, 10);

Math.sqrt( x )

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

Example

Math.sqrt(64);

Math.abs( x )

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

Example

Math.abs(-4.7);

Math.pow( x, y )

The Math.pow(x, y) method returns the value of x raised to the power of y:

Example

Math.pow(2, 8);  // 256.0

Note: Math.pow(2, 8) means 2 multiplied by itself 8 times: 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 = 256 Note: The Math.pow() method always returns a double, even if the result is a whole number. For example, Math.pow(2, 8) returns 256.0 (not 256).


Rounding Methods

Java has several methods for rounding numbers:

  • Math.round(x) - rounds to the nearest integer
  • Math.ceil(x) - rounds up (returns the smallest integer greater than or equal to x)
  • Math.floor(x) - rounds down (returns the largest integer less than or equal to x)

Example

Math.round(4.6);  // 5
Math.ceil(4.1);   // 5.0
Math.floor(4.9);  // 4.0

Random Numbers

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

Example

Math.random();

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

Note: Math.random() returns a double. To get an integer, you need to cast it with (int).


Complete Math Reference

For a complete reference of Math methods, go to our Java Math Methods Reference.

Want to go beyond the notes?

Join CodingNow 2.0's Java course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Java Math – FAQs

Quick answers about learning Java Math in Java.

This free note from CodingNow 2.0 explains Java Math in Java — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Java topic on CodingNow 2.0, including Java Math, is 100% free with no signup required.
With focused practice, most students grasp Java Math in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now