×

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

How to Create Singleton Class in Java

In this tutorial, we will discuss the singleton class and how to create it. Introduction Java is a purely object-oriented programming language. It consists of classes and objects. But Singleton is one...

4 minutes read.

Types of Assignment Operators in Java

In this tutorial, we are going to study assignment operators and their types in Java language. Before proceeding to the types, let us know the term ‘assignment operator’.  The assignment...

5 minutes read.

Rehashing in Java

The most crucial idea mostly in the data structure is hashing, which is utilized to change a particular key into some other value. The hash function can be used to...

7 minutes read.

Java Location

PATH is an environmental variable that the system software now uses to find executable files (.exe) or Java binaries ( java or javac command). Once chosen, a route cannot be...

3 minutes read.

Matrix Multiplication Program in Java

Matrix Multiplication Program in Java The matrix multiplication program in Java is the continuation of the matrix program in Java that we have already discussed earlier. In this section, we will...

3 minutes read.

Statements in java

What is Statement in java: A statement in java is an instruction that explains what will happen based on the condition. Types of java statements: There are different statements in java Expression statementDeclaration statementControl...

2 minutes read.

How to calculate time difference in Java

In this tutorial, we learned about how to calculate time differences in java language and which methods use to find the difference and its example. Prerequisite To understand this example, you need...

5 minutes read.

Checked vs Unchecked Exceptions in Java

In this tutorial, we will discuss Java's checked and unchecked Exceptions. Exception An exception is an undesirable event that disrupts the normal flow of the program. An exception is thrown at runtime. It...

4 minutes read.

Pernicious Number in Java

If the number of 1s in a number appearing in the binary representation is the prime number, such a number is known as a pernicious number. A pernicious number always corresponds to...

4 minutes read.

Null Pointer Exception in Java

It is a runtime error exception. The null value is allocated to the object reference in this exception. We will explicitly throw this null pointer exception when the program wants...

3 minutes read.

Command Class in Java

We use the command class to run commands against the database. The Command class may find a set of parameter objects for use in sending values to a stored procedure...

3 minutes read.

Convert JSON File to String in Java

Before understanding the conversion of JSON file to string, one must know about JSON. What is JSON? JSON stands for JavaScript Object Notation. It is an open standard format lightweight, text-based, and...

3 minutes read.

Java Obfuscator

Obfuscation is the process of making something unclear or difficult to interpret. Obfuscators are being used in programming to protect the source code from hackers. In this article, we will...

8 minutes read.

Non-primitive data types in Java

The kind of data stored in the variable is determined by its type. The type describes the data category (different sizes and values). These are not already built into the devices....

4 minutes read.

Trim Method in String Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

3 minutes read.

Sleeping Barber Problem in Java

The barbershop in this issue has one barber, one barber chair, and N chairs for customers in the waiting area. We may demonstrate the issue by keeping with the original...

6 minutes read.

All the important string methods in Java

What is a String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Nested Enum in Java

A class that can be defined within another class is called a nested class in Java. You can logically group classes that are used onlyin one place. This makes encapsulation...

3 minutes read.

Scanner in Java

Static way of Programming: When a variable can’t change its value during run time is called a Static way of programming. In this programming a variable is directly assigned to a...

4 minutes read.

Sort Elements by Frequency in Java

To sort the elements in Java by using frequency, we need an input array. We should create a function that sorts the elements in an array by using their frequencies...

3 minutes read.