java.lang.Math.decrementExact()
method is used to find the argument decremented by one of a long value for the given input (x
– parameter) in Java.
throwing an exception if the result overflows a long.
Package
decrementExact()
method exists in Math
class of java.lang
package.Since: JDK1.8
Method Declaration of decrementExact()
Method
The declaration of
decrementExact()
method is:
public static long decrementExact(long x)
Syntax of decrementExact()
Method
The syntax of
decrementExact()
method in Java is:
java.lang.Math.decrementExact(long x)
Example
/* *Find the argument decremented by one of given input(long) without importing the java.lang Package. */ public class JavaMathExample { public static void main(String[] args) { //Declaring variables long x = -4876.1874; long result; //Assign argument decremented by one of x to result variable result = java.lang.Math.decrementExact(x); //Printing result to the console System.out.println(result); } }
Output:
Example
By importing java.lang
package the syntax of decrementExact()
method can be used as:
/* *Find the argument decremented by one of given input(long) by importing the java.lang Package. */ import java.lang.*; //importing package public class JavaMathExample { public static void main(String[] args) { //Declaring variables long x = -68.97; long result; //Assign argument decremented by one of x to result variable result = Math.decrementExact(x); //Printing result to the console System.out.println(result); } }
Output:
Special Cases of decrementExact()
Method in Java
ArithmeticException – if the result overflows a long
Example
Example
/* *This example shows some special cases of decrementExact() method in Java */ import java.lang.*; //importing java.lang package public class JavaMathExample { public static void main(String[] args) { //Declaring variables long positiveZero = 0; long negativeZero = -0; long positiveInfinity = Long.POSITIVE_INFINITY; long negativeInfinity = Long.NEGATIVE_INFINITY; long nan = Double.NaN; long result; //Here argument is positive zero, output will also be positive zero result = Math.decrementExact(positiveZero); System.out.println(result); //Here argument is negative zero, output will be positive zero result = Math.decrementExact(negativeZero); System.out.println(result); //Here argument is positive infinity, output will also be positive infinity result = Math.decrementExact(positiveInfinity); System.out.println(result); //Here argument is negative infinity, output will be negative infinity result = Math.decrementExact(negativeInfinity); System.out.println(result); //Here argument is NaN, output will be NaN result = Math.decrementExact(nan); System.out.println(result); } }
Output:
Parameters of decrementExact()
Method in Java
x
– Where x
is any valid long input value. This parameter is required.
Error Handling
If the
If there is no argument (
x
parameter is not a number decrementExact()
method returns an error
.If there is no argument (
x
– input value) passes to the method, the compiler will produce an error
.Example
/* *This example returns a compile time error */ import java.lang.*; //importing java.lang package public class JavaMathExample { public static void main(String[] args) { //Declaring variables String x = "Not a number"; long result; //argument is not a number result = Math.decrementExact(x); System.out.println(result); //Here no argument is used result = Math.decrementExact(); System.out.println(result); } }
Output:
Error
Return Value of decrementExact()
Method in Java
decrementExact()
method returns the argument decremented by one of a long value for the given input (x
– parameter).
Java decrementExact()
Method Example 1
import java.lang.*; //importing java.lang package public class JavaMathExample { public static void main(String[] args) { long negativeValue = -18.869; long positiveValue = 27.78639; long zero = 0; System.out.println("argument decremented by one of a Negative Value: "+Math.decrementExact(negativeValue)); System.out.println("argument decremented by one of a Positive Value: "+Math.decrementExact(positiveValue)); System.out.println("argument decremented by one of Zero: "+Math.decrementExact(zero)); } }
Output:
Java decrementExact()
Method Example 2
import java.lang.*; //importing java.lang package public class JavaMathExample { public static void main(String[] args) { //Declare and initialize an array containing long values long test [] = { Long.MAX_VALUE, Long.MIN_VALUE, Long.NaN, Long.MIN_EXPONENT, 42.867e-12, 16.852, 0, -2.58, -78.8569e7 }; //Use for loop to print argument decremented by one of each element of the array for (int i=0; i < test.length; i++) { System.out.println( Math.decrementExact(test[i]) ); } } }
Output: