How Java Math class works
You can carry out simple calculations with Java Math. It has different methods that cover logarithms and trigonometry, as well as all of the basics. The syntax is comparatively simple, making it easy to learn.
What is Java Math?
If you want to perform basic numerical calculations in Java, you can use its Math class. The Java class doesn’t need to be imported separately and has numerous methods which we’ll cover in more detail later on in this article.
The Math class is not instantiated, and its methods are only accessed statically. The two constants of the class are also static: Euler’s number (approximately e = 2.7182818284590), which is the basis for the natural logarithm and the natural exponential function, and the number Pi (approximately π = 3.1415926535). The programming language’s Math class is contained in the java.lang package, and calculation results of this class are usually of the double data type.
- 99.9% uptime
- PHP 8.3 with JIT compiler
- SSL, DDoS protection, and backups
How to perform different calculations with Java Math
The best way to understand the functionality and syntax of the Java class Math is to use examples. It’s easier to understand the class and how it’s used in the context of individual methods. Below, we’ve included a range of different calculations to show you how the class works.
Determine absolute values
If you want to determine the absolute value of a parameter, you can use abs()
. An absolute value is the distance between a number and 0 or an unsigned number. This means the result will always be positive. The data types permitted for this method are double, float, int and long. Below, we’ll show you how Math.abs
works with a positive number. For the output in the examples that follow, we’ll use the Java command System.out.println
.
The output looks like this:
The initial value can also be negative. The result will still be positive. Let’s see what happens when we make the 7 in the above example negative:
The output is largely the same as that of the previous example:
The method ignores the sign of the negative integer (-7) and outputs 7 as the result.
Determine the largest value
Use the max()
method to determine the larger value of two inputs. Here’s how it works:
The output is:
Determine the smallest value
The code for determining a smaller value is similar to the code in the previous example. Use the method min()
to do this:
Here’s the output:
Calculate powers
While the previous examples were quite simple, there are more sophisticated calculations that the Java class Math can do. For example, you can calculate powers as well. The method for calculating powers is called pow(). With this method, we need to first define a base and an exponent before carrying out the calculation.
This is what the output will look like:
Calculate square roots
The class can also be used for square root calculations with the sqrt()
method. In the following example, we’ll calculate the square root of 64:
This is the output:
Generate random numbers
With the random()
method, Java generates a random number between 0.0 and 1.0 or in a range that you specify yourself.
A possible output would be:
However, you can also limit the possible results, for example, by only allowing whole numbers between 0 and 100. To do this, use the following code:
This will give you a random result like this:
What are the most important methods?
There are numerous methods that you can use with the Java Math class. We’ve listed the most important ones for you here:
Method | Function |
---|---|
abs()
|
Returns the absolute value of an argument |
max()
|
Returns the larger of two values |
min()
|
Returns the smaller of two values |
pow()
|
Returns the power value |
sqrt()
|
Calculates the square root |
random()
|
Returns a random double value |
cbrt()
|
Calculates the cube root |
log()
|
Returns the natural logarithm of a double value |
sin()
|
Calculates the sine of a double value |
cos()
|
Calculates the cosine of a double value |
tan()
|
Calculates the tangent value of a double value |
round()
|
Rounds a double value up or down to an integer |
negateExact()
|
Displays the opposite value of an argument |
floor()
|
Rounds down the largest double value that is less than or equal to the given argument |