Java Basics:Java Library Methods
Java Math Methods
Java has a lot of Math methods that allow us to perform mathematical computations. In this reference page, you will find all the math methods available in Java. For example, if you need to get the square root of a number, use the sqrt() method.
Java Math abs()
The Java Math abs() method returns the absolute value of the specified value.
The syntax of the abs()
method is:
Math.abs(num)
Here, abs()
is a static method. Hence, we are accessing the method using the class name, Math
.
abs() Parameters
The abs()
method takes a single parameter.
- num - number whose absolute value is to be returned. The number can be:
int
double
float
long
abs() Return Value
- returns the absolute value of the specified number
- returns the positive value if the specified number is negative
Example 1: Java Math abs() with Positive Numbers
import java.lang.Math;
class Main {
public static void main(String[] args) {
// create variables
int a = 7;
long b = 23333343;
double c = 9.6777777;
float d = 9.9f;
// print the absolute value
System.out.println(Math.abs(a)); // 7
System.out.println(Math.abs(c)); // 9.6777777
// print the value without negative sign
System.out.println(Math.abs(b)); // 23333343
System.out.println(Math.abs(d)); // 9.9
}
}
In the above example, we have imported the java.lang.Math
package. This is important if we want to use methods of the Math
class. Notice the expression,
Math.abs(a)
Here, we have directly used the class name to call the method. It is because abs()
is a static method.
Example 2: Java Math abs() with Negative Numbers
import java.lang.Math;
class Main {
public static void main(String[] args) {
// create variables
int a = -35;
long b = -141224423L;
double c = -9.6777777d;
float d = -7.7f;
// get the absolute value
System.out.println(Math.abs(a)); // 35
System.out.println(Math.abs(b)); // 141224423
System.out.println(Math.abs(c)); // 9.6777777
System.out.println(Math.abs(d)); // 7.7
}
}
Here, we can see that the abs()
method converts the negative value into a positive value.
____________________________________________________________
Java Math addExact()
The Java Math addExact() method adds the specified numbers and returns it.
The syntax of the addExact()
method is:
Math.addExact(num1, num2)
Here, addExact()
is a static method. Hence, we are accessing the method using the class name, Math
.
addExact() Parameters
The addExact()
method takes two parameters.
- num1 - value that is added to num2
- num2 - value that is added to num1
Note: The data type of both values should be either int
or long
.
addExact() Return Value
- returns the sum of two values
Example 1: Java Math addExact()
import java.lang.Math;
class Main {
public static void main(String[] args) {
// create int variable
int a = 24;
int b = 33;
// addExact() with int arguments
System.out.println(Math.addExact(a, b)); // 57
// create long variable
long c = 12345678l;
long d = 987654321l;
// addExact() with long arguments
System.out.println(Math.addExact(c, d)); // 999999999
}
}
In the above example, we have used the Math.addExact()
method with the int
and long
variables to calculate the sum.
The addExact()
method throws an exception if the result of addition overflows the data type. That is, the result should be within the range of the data type of specified variables.
import java.lang.Math;
class Main {
public static void main(String[] args) {
// create int variable
// maximum int value
int a = 2147483647;
int b = 1;
// addExact() with int arguments
// throws exception
System.out.println(Math.addExact(a, b));
}
}
In the above example, the value of a is the maximum int
value and the value of b is 1. When we add a and b,
2147483647 + 1
=> 2147483648 // out of range of int type
Hence, the addExact()
method throws the integer overflow
exception.
- ________________________________-________________________
Comments
Post a Comment