W3Schools Java Math Class: Master The Math Class In Java

Robert Jul 13, 2026

W3Schools' Java Math Class is a comprehensive learning resource for aspiring Java developers seeking to enhance their mathematical operations understanding. This platform offers interactive tutorials, examples, and a reference manual, fostering an engaging learning experience. Let's explore the key aspects of W3Schools' Java Math Class.

MATH FUNCTIONS IN JAVA
MATH FUNCTIONS IN JAVA

W3SchoolsJan 11, 2022ย ยท Java Math Class - Tutorial.w3schools.com

W3Schools.com
W3Schools.com

Understanding Java Math Class

Java's built-in Math class provides a myriad of methods for performing common mathematical tasks, including trigonometric functions, rounding, and generation of random numbers. This class is final and cannot be instantiated, with its fields and methods marked as static.

80+ Pattern Programs In Java
80+ Pattern Programs In Java

To begin, include the Math class in your Java program using the import statement:

import java.lang.Math;

Java Cheat Sheet: Operators, Scanner & User Input โ˜•
Java Cheat Sheet: Operators, Scanner & User Input โ˜•

Basic Mathematical Operations

W3Schools' Java Math Class tutorials commence with essential arithmetic operations. Here's how you can use the Math class for addition, subtraction, multiplication, and division:

int a = 40, b = 10;
System.out.println("Addition: " + Math.addExact(a, b));
System.out.println("Subtraction: " + Math.subtractExact(a, b));
System.out.println("Multiplication: " + Math.multiplyExact(a, b));
System.out.println("Division: " + Math.divideExact(a, b));

a black background with text that reads math with java perform addition, subtraction, multiplication, division, and modulus with numbers
a black background with text that reads math with java perform addition, subtraction, multiplication, division, and modulus with numbers

Rounding Numbers

The Math class offers multiple methods for rounding numbers, including round(), ceil(), floor(), and rint(). Here's an example demonstrating their use:

double num = 42.74;
System.out.println("Rounded: " + Math.round(num));
System.out.println("Ceiled: " + Math.ceil(num));
System.out.println("Floored: " + Math.floor(num));
System.out.println("Rounded with `rint()`: " + Math.rint(num));

What is number class in Java?
What is number class in Java?

Exploring Trigonometric Functions

The Math class also supplies methods for trigonometric functions, enabling calculations of sine, cosine, tangent, and more. These functions operate on an angle, measured in radians:

Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet ๐ŸŽฏ๐Ÿ’ป
Java Scanner Class Made Easy: Your Ultimate Input Cheat Sheet ๐ŸŽฏ๐Ÿ’ป
Data Science, Science
Data Science, Science
Differential Equations Short Notes ๐Ÿ“˜ | JEE Main Maths Revision | Handwritten Notes
Differential Equations Short Notes ๐Ÿ“˜ | JEE Main Maths Revision | Handwritten Notes
Deep Dive into java numbers-  Methods with syntax and examples
Deep Dive into java numbers- Methods with syntax and examples
Master Two Pointers Technique ๐Ÿ’ป | Java DSA Interview Preparation Guide
Master Two Pointers Technique ๐Ÿ’ป | Java DSA Interview Preparation Guide
Difference between Method and constructor in Java - Codeforcoding
Difference between Method and constructor in Java - Codeforcoding
Java Class Inheritance
Java Class Inheritance
๐ŸŒŸ โ˜•Java User Input Example | Learn Java Programming Easily
๐ŸŒŸ โ˜•Java User Input Example | Learn Java Programming Easily
Best Java IDEs in 2020
Best Java IDEs in 2020
a diagram showing the different classes in college
a diagram showing the different classes in college
How to Declare Classes in Java? | Java Declaring Classes | Java Tutorial
How to Declare Classes in Java? | Java Declaring Classes | Java Tutorial
three types of variable type in an organization
three types of variable type in an organization
Java OOP concepts cheat sheet
Java OOP concepts cheat sheet
Top 12 Java 8 Stream Examples for Beginners
Top 12 Java 8 Stream Examples for Beginners
w3schools java math class
w3schools java math class
Java Programming Essentials: A Guide to Mastery with Java Course in Pune
Java Programming Essentials: A Guide to Mastery with Java Course in Pune
What are Arrays in Java?
What are Arrays in Java?
the calculator worksheet is shown in this image
the calculator worksheet is shown in this image
Top Features of Java Every Beginner Should Know โ˜•๐Ÿš€
Top Features of Java Every Beginner Should Know โ˜•๐Ÿš€

Sine, Cosine, and Tangent

Calculate sine, cosine, and tangent using Math's sin(), cos(), and tan() methods:

double angle = Math.PI / 4;
System.out.println("Sin: " + Math.sin(angle));
System.out.println("Cos: " + Math.cos(angle));
System.out.println("Tan: " + Math.tan(angle));

Hyperbolic Functions

The Math class also provides hyperbolic functions โ€“ sinh(), cosh(), tanh(), coth(), asinh(), acosh(), atanh(), and acoth(). Here's an example:

double x = 1;
System.out.println("Sinh: " + Math.sinh(x));
System.out.println("Cosh: " + Math.cosh(x));
System.out.println("Tanh: " + Math.tanh(x));

Dive into the wealth of resources offered by W3Schools' Java Math Class, from manipulating numbers to generating random values. Enhance your Java proficiency today and explore the world of mathematical operations!