CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Math class in Java

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

Math class in Java

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 ยท 3 min read ยท 0 Comments
Share: in X

The java.lang.Math class is a built-in class in Java that provides methods for performing basic numeric operations. This class is located in the java.lang package, so we do not need to import it in order to use it. All of the methods in the Math class are static, so we do not need to create an instance of the Math class to use them.

Table of Contents

Operations in Math class in Java
Find absolute value of a number in Java
Find max and min values in Java
Round value in Java
Find square root of value in Java
Find sin cos and tan values in Java
Generate random number in Java
Conclusion

Operations in Math class in Java

The Math class includes methods for the following operations:

  • Absolute value
  • Maximum and minimum of two values
  • Rounding
  • Square root
  • Trigonometric functions (sine, cosine, and tangent)
  • Random number generation

Find absolute value of a number in Java

Following is an example of how to use the Math class to find the absolute value of a number:

double num = -9.0;
double absNum = Math.abs(num);
System.out.println("Absolute value of " + num + " is " + absNum);

Output:

Absolute value of -9.0 is 9.0

Find max and min values in Java

We can use the max and min methods to find the greater or smaller of two values, respectively. For example:

double num1 = 9.0;
double num2 = 3.0;
double max = Math.max(num1, num2);
double min = Math.min(num1, num2);
System.out.println("Greater number between " + num1 + " and " + num2 + " is " + max);
System.out.println("Smaller number between " + num1 + " and " + num2 + " is " + min);

Output:

Greater number between 9.0 and 3.0 is 9.0
Smaller number between 9.0 and 3.0 is 3.0

Round value in Java

The round method allows us to round a float value to the nearest integer. For example:

float num = 9.6f;
int roundedNum = Math.round(num);
System.out.println("Rounded value of " + num + " is " + roundedNum);

Output:

Rounded value of 9.6 is 10

Find square root of value in Java

The sqrt method returns the square root of a given value. For example:

double num = 9.0;
double sqrtNum = Math.sqrt(num);
System.out.println("Square root of " + num + " is " + sqrtNum);

Output:

Square root of 9.0 is 3.0

Find sin cos and tan values in Java

The sin, cos, and tan methods allow us to calculate the sine, cosine, and tangent of a given value, respectively. These methods expect the argument to be in radians, not degrees. We can use the toRadians method of the Math class to convert from degrees to radians if necessary.

For example:

double num = 90.0;
double radians = Math.toRadians(num);
double sinNum = Math.sin(radians);
double cosNum = Math.cos(radians);
double tanNum = Math.
tan(radians);
System.out.println("Sine of " + num + " degrees is " + sinNum);
System.out.println("Cosine of " + num + " degrees is " + cosNum);
System.out.println("Tangent of " + num + " degrees is " + tanNum);

Output:

Sine of 90.0 degrees is 1.0
Cosine of 90.0 degrees is 6.123233995736766E-17
Tangent of 90.0 degrees is 1.633123935319537E16

Generate random number in Java

Finally, the random method generates a random double value between 0 (inclusive) and 1 (exclusive). We can use this method to generate random numbers for various purposes, such as shuffling an array or simulating dice rolls.

For example:

double random = Math.random();
System.out.println("Random number: " + random);

Output:

Random number: 0.7064755872309822

You can visit the post how to generate a random number in Java which provides a detailed overview of random number generation.

Conclusion

In this post, we learned various methods available in Math class. Math class in Java is a very important class that helps us to do basic numerical operations.

Related Posts:

  • How to Generate Random Number in Java?
  • Packages in Java: A Guide to Modular, Maintainable Code
  • Control Statements in Java
  • Abstract Class in Java: Bridging Code Reusability…
  • MySQL Commands for Developers
  • Top 10 Common Java Errors
Tags:java
Was this article helpful?
โ† Previous ArticleBevelBorder in Java Swing
Next Article โ†’Server Side Programming in Java

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap