×

Java Math decrementExact() Method

The decrementExact() method of Math class returns the argument which is decremented by one, throwing an exception is the result overflows an int or long.

Syntax:

public static int decrementExact (int a)

Parameters:

The parameter ‘a’ represents the value to decrement.

Return Value:

The decrementExact () method returns the result after decrementing the value.

Throws:

The decrementExact () method throws:

ArithmeticException- if the result overflows an int or a long

Example 1:

public class JavaMathDecrementExactExample1 {
    public static void main(String[] args) {
        int a=90;
        //returns the result after decrementing the int value
        System.out.println("Decremented value : "+Math.decrementExact(a));
    }
}

Output:

Decremented value : 89

Example 2:

public class JavaMathDecrementExactExample2 {
    public static void main(String[] args) {
        //type cast the value Math.PI into int
        int a= (int) Math.PI;
        System.out.println("Cosine value for "+a+" = "+Math.decrementExact(a));
    }
}

Output:

Cosine value for 3 = 2

Example 3:

public class JavaMathDecrementExactExample3 {
    public static void main(String[] args) {
        //returns the result after decrementing the long value
        long x = Long.MAX_VALUE;
        System.out.println("Decremented value for "+x+":
"+Math.decrementExact(x));
    }
}

Output:

Decremented value for 9223372036854775807: 9223372036854775806

Example 4:

public class JavaMathDecrementExactExample4 {
    public static void main(String[] args) {
        //returns an exception is the result overflows a long
long x = Long.MIN_VALUE;
        System.out.println("Decremented value for "+x+":
"+Math.decrementExact(x));
    }
}

Output:

Exception in thread "main" java.lang.ArithmeticException: long overflow
            at java.lang.Math.decrementExact(Math.java:960)
            at com.Tutorials And
Examples.JavaMathDecrementExactExample4.main
(JavaMathDecrementExactExample4.java:7)

Related Topics

Java Integer valueOf() method

The valueOf() method of Java Integer class returns an Integer object holding the specified int value. The second method returns an Integer object holding the specified String value. The third syntax returns...

2 minutes read.

Java Linters

When it comes to programming, everyone makes mistakes. Errors are bad for developers since they are difficult to handle. But handling as many as possible errors will bring out the...

6 minutes read.

Java Garbage Collection

Java Garbage Collection In Java, unreferenced objects are treated like garbage. The process of reclaiming the unused memory during runtime automatically is known as the Java Garbage Collection.In other words, the...

4 minutes read.

PriorityBlockingQueue Class in Java

What is the Queue? An abstract data structure like Stacks is a queue. A queue is open on both ends. Data is always pushed to one end, called enqueue, and removed...

4 minutes read.

Difference between C, C++, java

C Language: C language is a procedure oriented language. It has been invented by Dennis Ritchie in the year 1970. It is one of the computer programming language. The purpose of...

3 minutes read.

Java Buffered Writer

BufferWriter Class: It is used to write the data more efficiently. This class is present in the java.io package, it inherits the data from the Writer class. Writer class is...

4 minutes read.

Java LinkedHashSet

LinkedHashSet in Java with Example Java LinkedHashSet extends HashSet and Implements the Set interface. It doesn’t contain only duplicate values like HashSet. It also permits the null elements. It maintains the order...

5 minutes read.

Hourglass problem in Java

In this section, we will discuss the hourglass problem in Java.The aim is to find the largest sum of an hour glass given a 2D matrix. An hour glass is made...

2 minutes read.

Java Program to Print Permutations of String

A string is given and you need to print all the possible ways for that string. Permutation is arranging the characters of a string to get outputs from the given...

3 minutes read.

Java New Keyword

To create a class instance in Java, use the new keyword. In other words, it returns a reference to the memory that was allocated for a new object and instantiates...

3 minutes read.

Anagram Program in Java using String

Anagram Program in Java Anagrams are those words that are generated by rearrangement of the letters of another phrase or word. Usually, while doing the rearrangement, the original letters are used...

8 minutes read.

Difference Between in Java and C++

FeatureC++JavaDefinitionC++ is a general programming language created by Bjarne Stroustrup as an extension of c language    Java is class-based, object-based, and designed to have as few implementation dependencies as...

4 minutes read.

Java Wrapper classes

Java Wrapper classes A wrapper class is a class whose object contains a primitive data type; moreover it provides a way to use primitive data type (int, boolean, etc.) as objects. Wrapper...

2 minutes read.

Java Queue Interface

Queue interface is a subtype of Collection interface. All methods in the Collection interface are also available in the Queue interface. It provides operations of Collection and also some additional...

2 minutes read.

How to uninstall the Java in Ubuntu

We need java on our computers in our daily lives because there are lots of different applications that are developed using the java environment, so we have to install java...

4 minutes read.

Program to find and replace characters on string in java

In JAVA, Strings are immutable. To overcome this Java String class contains a lot of methods to do operations on Strings. One such method is replace method. String Replace It returns a...

3 minutes read.

Java Private keyword

A Java access modifier is a private keyword. It can be used to inner classes, methods, and variables. It is the type of access modifier that is most constrained. Privately declared...

4 minutes read.

Java String copyValueOf() method

copyValueOf() method returns a String that holds the character sequence of the character array. Syntax: copyValueOf(char[] data) Parameters: data : the character array i.e. String Returns: It returns a String that contains the characters of the...

2 minutes read.

Beautiful Array in Java

In this section, we will be acknowledged about the beautiful array in Java, its characteristics, how it is achieved. What are the types of approaches and what are the tools...

8 minutes read.

Java Math round() Method

The round() method of Java Math class returns a long or an int value that is closest to the argument and is rounded to positive infinity. Syntax: public static int round(float a)public...

2 minutes read.