×

Java Math incrementExact() Method

The incrementExact() method of Math class returns the argument incremented by one, throwing an exception if the result overflows an int or a long.

Syntax:

  1. public static int incrementExact (int a)
  2. public static int incrementExact (long a)

Parameters:

The parameters ‘f1’ and ‘f2’ represent the dividend and the divisor.

Return Value:

The incrementExact () method returns the result after incrementing the argument by one.

Throws:

The incrementExact () method throws:

ArithmeticException – if the result overflows an int or long

Example 1:

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

Output:

Incremented value : 91

Example 2:

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

Output:

Incremented value for 3.141592653589793 = 4

Example 3:

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

Output:

Exception in thread "main" java.lang.ArithmeticException: long overflow
at java.lang.Math.incrementExact(Math.java:926)
at com.TutorialsAndExamples.JavaMathIncrementExactExample3.main
(JavaMathIncrementExactExample3.java:7)

Example 4:

public class JavaMathIncrementExactExample4 {
    public static void main(String[] args) {
        //returns the result after incrementing the long value
        long x = Long.MIN_VALUE;
        System.out.println("Incremented value for "+x+":
"+Math.incrementExact(x));
    }
}

Output:

Incremented value for -9223372036854775808: -9223372036854775807

Related Topics

HashMap Vs HashTable

HashMap HashMap is the basic implementation of the map interface in Java. HashMap stores the data in key and value pairs. Keys are used to access the value of the element. It...

5 minutes read.

Figurate Number in Java

There have been several uses for figurate or figural numerals throughout history. A number that may be expressed by regular, distinct geometric shapes with spaced evenly points is referred to...

4 minutes read.

Java try catch

Java try catch There can be statements that can cause exceptions, and the exception leads to abnormal termination of the program. To avoid that abnormal termination, those statements that look potent...

4 minutes read.

How to Convert Integer to String in Java

How to Convert int to String in Java It is used when you want to convert an integer to String. You can convert int to String by using the following methods: Using...

3 minutes read.

Generics vs Wildcard in Java

In generic programming, the question mark (?) is often referred to as the wildcard. It stands for a mysterious type. The wildcard can be used for many different contexts, such as...

4 minutes read.

Java Framework List

The framework is the programs which are written in Java. Frameworks in Java are used to create web applications. The code which can reuse can act as a reference for...

6 minutes read.

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 String lastIndexOf() method

Java String lastIndexOf() method returns last index of character or substring in a String. Syntax: Method Description int lastIndexOf(int ch)It returns last index position for the given char valueint lastIndexOf(int ch, int...

2 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.

How to Convert String to long in Java

How to Convert String to long in java It is used when you want to perform the mathematical operation on the String which contains long number then the conversion from String...

4 minutes read.

Java Binary Tree

The non-linear data structure known as a binary tree is a type of tree, and because it stores data in a hierarchical manner, it is mostly utilised for finding and...

7 minutes read.

AES 256 Encryption in Java

Now a days, security has grown in importance. Java programming supports a variety of encryption and hashing methods, which offers security for data transport and communication among various nodes. In...

4 minutes read.

Stack in Java

Java provides a number of collection frameworks to store the collection of objects. Among the collection of data structures " Stack " is one of them. Stack is one of...

5 minutes read.

Java Output Formatting

Sometimes we want a program's output to be displayed in a specific format. The printf () function in the C programming language can be used to achieve this. We will...

4 minutes read.

Java array list remove time complexity

Java: We know that java is one of the programming languages. The main feature of java which is not in C or object oriented programming language is platform independence. Not only the...

7 minutes read.

Untouchable Number in Java

If a number N cannot be divided properly by any positive number, it is said to be an untouchable number. Additionally known as nonaliquot numbers. The sequence is A005114 from...

3 minutes read.

Java Integer class

The Integer class wraps a primitive int type value in an object. Its object contains only a single field whose type is int. Methods: The java.lang.Integer class provides several different methods for...

4 minutes read.

Differences and Similarities between HashSet, LinkedHashSet and TreeSet in Java

HashSet Class: The HashSet is a class that executes the Setpoint of interaction. It is utilised to store the items in a hashtable; a hashtable is an information structure which holds...

10 minutes read.

if-else Program in Java

if-else Program in Java The if-else program in Java controls which code snippet will execute. The if-else program is very basic and yet very important. Because it checks how well one...

19 minutes read.

Duodecimal in Java

Duodecimal is a notation style in which a number with a base of 12 is referred to be a duodecimal number. In Java, we can use to convert duodecimal integers...

2 minutes read.