codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

63 / 65 topics
61Java Keywords62Java String Methods63Java Math Methods64Java Arrays Methods65Java Collections Methods
Tutorials/Java Programming/Java Math Methods
☕Java Programming

Java Math Methods

Updated 2026-05-12
30 min read

Java Math Methods

In the world of programming, performing mathematical calculations is a common task. Java provides a robust set of methods within the Math class to handle these operations efficiently. This tutorial will explore major Math methods in Java, providing clear explanations and practical examples.

Introduction

The Math class in Java offers a wide range of static methods for basic arithmetic, trigonometry, logarithms, and more. These methods are essential for developers who need to perform complex calculations or manipulate numbers in their applications. Understanding these methods will enhance your ability to write efficient and accurate code.

Core Content

Basic Arithmetic Operations

Math.abs()

The Math.abs() method returns the absolute value of a number, which is its non-negative value without regard to its sign.

AbsExample.java
1public class AbsExample {
2 public static void main(String[] args) {
3 int num1 = -5;
4 double num2 = -3.14;
5
6 System.out.println("Absolute of " + num1 + " is: " + Math.abs(num1));
7 System.out.println("Absolute of " + num2 + " is: " + Math.abs(num2));
8 };
9}
Output
Absolute of -5 is: 5
Absolute of -3.14 is: 3.14

Math.max()

The Math.max() method returns the larger of two numbers.

MaxExample.java
1public class MaxExample {
2 public static void main(String[] args) {
3 int num1 = 10;
4 int num2 = 20;
5
6 System.out.println("Maximum of " + num1 + " and " + num2 + " is: " + Math.max(num1, num2));
7 }
8}
Output
Maximum of 10 and 20 is: 20

Math.min()

The Math.min() method returns the smaller of two numbers.

MinExample.java
1public class MinExample {
2 public static void main(String[] args) {
3 int num1 = 10;
4 int num2 = 20;
5
6 System.out.println("Minimum of " + num1 + " and " + num2 + " is: " + Math.min(num1, num2));
7 }
8}
Output
Minimum of 10 and 20 is: 10

Rounding Methods

Math.round()

The Math.round() method rounds a floating-point number to the nearest integer.

RoundExample.java
1public class RoundExample {
2 public static void main(String[] args) {
3 double num = 3.6;
4 System.out.println("Rounded value of " + num + " is: " + Math.round(num));
5 }
6}
Output
Rounded value of 3.6 is: 4

Math.ceil()

The Math.ceil() method returns the smallest integer that is greater than or equal to a given number.

CeilExample.java
1public class CeilExample {
2 public static void main(String[] args) {
3 double num = 3.1;
4 System.out.println("Ceiling value of " + num + " is: " + Math.ceil(num));
5 }
6}
Output
Ceiling value of 3.1 is: 4.0

Math.floor()

The Math.floor() method returns the largest integer that is less than or equal to a given number.

FloorExample.java
1public class FloorExample {
2 public static void main(String[] args) {
3 double num = 3.9;
4 System.out.println("Floor value of " + num + " is: " + Math.floor(num));
5 }
6}
Output
Floor value of 3.9 is: 3.0

Power and Logarithm Methods

Math.pow()

The Math.pow() method returns the result of raising a number to a specified power.

PowExample.java
1public class PowExample {
2 public static void main(String[] args) {
3 double base = 2;
4 double exponent = 3;
5 System.out.println(base + " raised to the power of " + exponent + " is: " + Math.pow(base, exponent));
6 }
7}
Output
2.0 raised to the power of 3.0 is: 8.0

Math.log()

The Math.log() method returns the natural logarithm (base e) of a number.

LogExample.java
1public class LogExample {
2 public static void main(String[] args) {
3 double num = 10;
4 System.out.println("Natural log of " + num + " is: " + Math.log(num));
5 }
6}
Output
Natural log of 10.0 is: 2.302585092994046

Math.exp()

The Math.exp() method returns Euler's number (e) raised to the power of a given number.

ExpExample.java
1public class ExpExample {
2 public static void main(String[] args) {
3 double exponent = 2;
4 System.out.println("e raised to the power of " + exponent + " is: " + Math.exp(exponent));
5 }
6}
Output
e raised to the power of 2.0 is: 7.38905609893065

Trigonometric Methods

Math.sin()

The Math.sin() method returns the sine of an angle specified in radians.

SinExample.java
1public class SinExample {
2 public static void main(String[] args) {
3 double angle = Math.toRadians(30); // Convert 30 degrees to radians
4 System.out.println("Sine of 30 degrees is: " + Math.sin(angle));
5 }
6}
Output
Sine of 30 degrees is: 0.49999999999999994

Math.cos()

The Math.cos() method returns the cosine of an angle specified in radians.

CosExample.java
1public class CosExample {
2 public static void main(String[] args) {
3 double angle = Math.toRadians(60); // Convert 60 degrees to radians
4 System.out.println("Cosine of 60 degrees is: " + Math.cos(angle));
5 }
6}
Output
Cosine of 60 degrees is: 0.5000000000000001

Math.tan()

The Math.tan() method returns the tangent of an angle specified in radians.

TanExample.java
1public class TanExample {
2 public static void main(String[] args) {
3 double angle = Math.toRadians(45); // Convert 45 degrees to radians
4 System.out.println("Tangent of 45 degrees is: " + Math.tan(angle));
5 }
6}
Output
Tangent of 45 degrees is: 0.9999999999999999

Random Number Generation

Math.random()

The Math.random() method returns a pseudorandom, uniformly distributed double value between 0.0 (inclusive) and 1.0 (exclusive).

RandomExample.java
1public class RandomExample {
2 public static void main(String[] args) {
3 System.out.println("Random number: " + Math.random());
4 }
5}
Output
Random number: 0.123456789 (example output)

Summary

MethodDescription
abs()Returns the absolute value of a number
max()Returns the larger of two numbers
min()Returns the smaller of two numbers
round()Rounds a floating-point number to the nearest integer
ceil()Returns the smallest integer greater than or equal to a number
floor()Returns the largest integer less than or equal to a number
pow()Raises a number to the power of another
log()Returns the natural logarithm of a number
exp()Returns Euler's number raised to the power of a number
sin(), cos(), tan()Trigonometric functions for sine, cosine, and tangent
random()Generates a pseudorandom double value between 0.0 and 1.0

Practical Example

Let's create a practical example that demonstrates the use of several Math methods to calculate the hypotenuse of a right-angled triangle using the Pythagorean theorem.

HypotenuseCalculator.java
1public class HypotenuseCalculator {
2 public static void main(String[] args) {
3 double sideA = 3.0;
4 double sideB = 4.0;
5
6 // Calculate hypotenuse using Math.sqrt() and Math.pow()
7 double hypotenuse = Math.sqrt(Math.pow(sideA, 2) + Math.pow(sideB, 2));
8
9 System.out.println("The length of the hypotenuse is: " + hypotenuse);
10 }
11}
Output
The length of the hypotenuse is: 5.0

What's Next?

Now that you have a solid understanding of Java Math methods, it's time to explore how arrays can be used to store and manipulate collections of data. In the next tutorial, we will delve into Java Arrays Methods, covering topics such as array declaration, initialization, traversal, and manipulation.

Stay tuned for more insightful tutorials on codingstuff.io!


PreviousJava String MethodsNext Java Arrays Methods

Recommended Gear

Java String MethodsJava Arrays Methods