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

11 / 65 topics
1Java Intro2Java Get Started3Java Syntax4Java Output5Java Comments6Java Variables7Java Data Types8Java Type Casting9Java Operators10Java Strings11Java Math12Java Booleans13Java If...Else14Java Switch15Java While Loop16Java For Loop17Java Break/Continue18Java Arrays
Tutorials/Java Programming/Java Math
☕Java Programming

Java Math

Updated 2026-05-12
15 min read

Java Math

In this tutorial, you will learn how to perform basic mathematical operations in Java using the Math class. The Math class provides a variety of methods for performing common mathematical calculations such as finding maximum and minimum values, calculating square roots, absolute values, and generating random numbers. These methods are essential for any programmer working with numerical data.

Introduction

The Math class is part of the java.lang package, which means it is automatically imported in every Java program. This makes it easy to use its methods without needing to import anything extra. Understanding how to utilize these methods can greatly simplify your code and make it more efficient.

Core Content

1. Finding Maximum and Minimum Values

The Math.max() and Math.min() methods are used to find the maximum and minimum of two numbers, respectively.

Example: Using Math.max() and Math.min()

MaxMinExample.java
1public class MaxMinExample {
2 public static void main(String[] args) {
3 int a = 10;
4 int b = 20;
5
6 int max = Math.max(a, b);
7 int min = Math.min(a, b);
8
9 System.out.println("Maximum value: " + max);
10 System.out.println("Minimum value: " + min);
11 };
12}
Output
Maximum value: 20
Minimum value: 10

2. Calculating Square Roots

The Math.sqrt() method is used to calculate the square root of a number.

Example: Using Math.sqrt()

SqrtExample.java
1public class SqrtExample {
2 public static void main(String[] args) {
3 double number = 25.0;
4 double sqrt = Math.sqrt(number);
5
6 System.out.println("Square root of " + number + " is: " + sqrt);
7 }
8}
Output
Square root of 25.0 is: 5.0

3. Calculating Absolute Values

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

Example: Using Math.abs()

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

4. Generating Random Numbers

The Math.random() method generates a random double value between 0.0 (inclusive) and 1.0 (exclusive). To generate random integers within a specific range, you can use additional calculations.

Example: Using Math.random() to Generate Random Integers

RandomExample.java
1public class RandomExample {
2 public static void main(String[] args) {
3 // Generate a random integer between 1 and 10
4 int randomNumber = (int)(Math.random() * 10) + 1;
5
6 System.out.println("Random number between 1 and 10: " + randomNumber);
7 }
8}
Output
Random number between 1 and 10: [random value between 1 and 10]

Practical Example

Let's create a practical example that uses all the methods we've learned to perform various mathematical operations.

Example: A Simple Calculator Program

SimpleCalculator.java
1public class SimpleCalculator {
2 public static void main(String[] args) {
3 int num1 = 15;
4 int num2 = 7;
5
6 // Find maximum and minimum
7 int max = Math.max(num1, num2);
8 int min = Math.min(num1, num2);
9
10 // Calculate square root of a number
11 double sqrtNum1 = Math.sqrt(num1);
12
13 // Calculate absolute value
14 int absNum2 = Math.abs(-num2);
15
16 // Generate a random number between 1 and 100
17 int randomNumber = (int)(Math.random() * 100) + 1;
18
19 System.out.println("Maximum of " + num1 + " and " + num2 + " is: " + max);
20 System.out.println("Minimum of " + num1 + " and " + num2 + " is: " + min);
21 System.out.println("Square root of " + num1 + " is: " + sqrtNum1);
22 System.out.println("Absolute value of -" + num2 + " is: " + absNum2);
23 System.out.println("Random number between 1 and 100: " + randomNumber);
24 }
25}
Output
Maximum of 15 and 7 is: 15
Minimum of 15 and 7 is: 7
Square root of 15 is: 3.872983346207417
Absolute value of -7 is: 7
Random number between 1 and 100: [random value between 1 and 100]

Summary

MethodDescription
Math.max(a, b)Returns the maximum of two numbers.
Math.min(a, b)Returns the minimum of two numbers.
Math.sqrt(x)Returns the square root of a number.
Math.abs(x)Returns the absolute value of a number.
Math.random()Generates a random double between 0.0 and 1.0.

What's Next?

In the next tutorial, we will explore Java Booleans and how to work with logical operations in Java. Understanding booleans is crucial for controlling program flow using conditional statements. Stay tuned!


PreviousJava StringsNext Java Booleans

Recommended Gear

Java StringsJava Booleans