Hi there, we’re Harisystems

"Unlock your potential and soar to new heights with our exclusive online courses! Ignite your passion, acquire valuable skills, and embrace limitless possibilities. Don't miss out on our limited-time sale - invest in yourself today and embark on a journey of personal and professional growth. Enroll now and shape your future with knowledge that lasts a lifetime!".

For corporate trainings, projects, and real world experience reach us. We believe that education should be accessible to all, regardless of geographical location or background.

1
1

Java Math: Performing Mathematical Operations

In Java, the Math class provides various methods for performing mathematical operations. These methods cover a wide range of mathematical functions, including basic arithmetic, trigonometry, logarithms, and more. In this article, we will explore the Math class in Java and provide examples to help you understand its usage.

Basic Arithmetic Operations

The Math class provides methods for performing basic arithmetic operations. Here are a few commonly used methods:

  • addExact(): Adds two integers and throws an exception if the result overflows. Example: int sum = Math.addExact(5, 3);
  • subtractExact(): Subtracts two integers and throws an exception if the result overflows. Example: int difference = Math.subtractExact(10, 5);
  • multiplyExact(): Multiplies two integers and throws an exception if the result overflows. Example: int product = Math.multiplyExact(2, 3);
  • incrementExact(): Increments an integer by 1 and throws an exception if the result overflows. Example: int number = Math.incrementExact(5);
  • decrementExact(): Decrements an integer by 1 and throws an exception if the result overflows. Example: int number = Math.decrementExact(5);

Trigonometric Functions

The Math class also provides trigonometric functions for working with angles. Here are a few commonly used trigonometric functions:

  • sin(): Returns the sine of an angle in radians. Example: double sineValue = Math.sin(Math.toRadians(45));
  • cos(): Returns the cosine of an angle in radians. Example: double cosineValue = Math.cos(Math.toRadians(60));
  • tan(): Returns the tangent of an angle in radians. Example: double tangentValue = Math.tan(Math.toRadians(30));

Exponential and Logarithmic Functions

The Math class provides exponential and logarithmic functions for working with numbers. Here are a few commonly used functions:

  • exp(): Returns the exponential value of a number. Example: double exponentialValue = Math.exp(2);
  • log(): Returns the natural logarithm (base e) of a number. Example: double logarithmValue = Math.log(10);
  • pow(): Returns the value of a number raised to the power of another number. Example: double powerValue = Math.pow(2, 3);
  • sqrt(): Returns the square root of a number. Example: double squareRootValue = Math.sqrt(16);

Rounding and Random Numbers

The Math class provides methods for rounding numbers and generating random numbers. Here are a few commonly used methods:

  • round(): Rounds a floating-point number to the nearest integer. Example: int roundedValue = Math.round(3.5);
  • floor(): Returns the largest integer less than or equal to a number. Example: double floorValue = Math.floor(4.7);
  • ceil(): Returns the smallest integer greater than or equal to a number. Example: double ceilValue = Math.ceil(4.2);
  • random(): Generates a random double value between 0.0 and 1.0. Example: double randomValue = Math.random();

Conclusion

The Math class in Java provides a wide range of methods for performing mathematical operations. In this article, we explored some of the commonly used methods for basic arithmetic, trigonometry, exponential and logarithmic functions, rounding, and generating random numbers. By leveraging the Math class, you can perform complex mathematical calculations and manipulate numbers in your Java programs. Continuously practice using the Math class and explore more advanced topics, such as numerical algorithms and libraries, to enhance your mathematical capabilities in Java programming.

4.5L

Learners

20+

Instructors

50+

Courses

6.0L

Course enrollments

4.5/5.0 5(Based on 4265 ratings)

Future Trending Courses

When selecting, a course, Here are a few areas that are expected to be in demand in the future:.

Beginner

The Python Course: Absolute Beginners for strong Fundamentals

By: Sekhar Metla
4.5 (13,245)
Intermediate

JavaScript Masterclass for Beginner to Expert: Bootcamp

By: Sekhar Metla
4.5 (9,300)
Intermediate

Python Coding Intermediate: OOPs, Classes, and Methods

By: Sekhar Metla
(11,145)
Intermediate

Microsoft: SQL Server Bootcamp 2023: Go from Zero to Hero

By: Sekhar Metla
4.5 (7,700)
Excel course

Future Learning for all

If you’re passionate and ready to dive in, we’d love to join 1:1 classes for you. We’re committed to support our learners and professionals their development and well-being.

View Courses

Most Popular Course topics

These are the most popular course topics among Software Courses for learners