×

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

Method and Block Synchronization in Java

The Synchronization is performed in multi-threading concept. The multi-threading is a concept of parallel running of a program for the execution. In the multi-threading concept, the threads are run by...

3 minutes read.

How to check if date is valid in Java?

In this article, you will acknowledge about how to verify if a date is valid or not. For this you will learn the approach, you will be able to write...

3 minutes read.

Add Time in Java

Before Java 8, java.util.Date was one of the most usually involved classes for addressing date-time values in java. Then Java 8 presented java.time.LocalDateTime and java.time.ZonedDateTime. Java 8 likewise permits us...

6 minutes read.

Pangram Program in Java

If a string comprises all alphabet letters from A to Z or from a to z without regard to case, it is referred to as a pangram. Some examples of pangram...

3 minutes read.

Java 8 filters list

A stream with the components of this stream that match the given predicate is provided by the streaming filter (Predicate predicate). This process is step-by-step. Because these operations are always...

4 minutes read.

How to find the length of an Array in Java

Arrays: An array is a sort of container object that stores constant quantities of values of a single type in one memory area. A finite number of items must all be...

3 minutes read.

Java vs JavaScript

Java Vs JavaScript Java and JavaScript, both play an important role in the field of Computer Technology. Most people think JavaScript is a part of Java. But it is not entirely...

4 minutes read.

How to add double quotes in a string in Java

Strings are indicated using double-quotes. Double quotes are not printed; the values inside the double quotes are printed. There are different methods for adding double quotes to the string, such...

2 minutes read.

Class memory in Java

Anything specifically defined in the Java code is stored either inside the heap or the stack, which is particularly significant. Stack and heap are the two kinds of memory available...

6 minutes read.

Java Create File

A File is a hypothetical way, which has no genuine presence. It is very much like while "using" that File that the major real activity of putting away something in...

5 minutes read.

How to check data type in Java

In this section, we will learn about the methodology to verify the data type in Java. There are mainly two methods in java called as getClass() and getName(). They are...

3 minutes read.

Java Pi

What is Pi? There are many formulas in geometry that employ the Pi constant to calculate things like circumference, area, and volume. A circle's circumference divided by its diameter yields a...

3 minutes read.

How to Convert Octal to Decimal in Java

How to Convert Octal to Decimal in Java There are two methods to convert Octal to Decimal: Using parseInt() method Using user-defined logic Using Integer.parseInt() method The Integer.parseInt() method is a static method...

2 minutes read.

Java Pop

The array, linked list, stack, queue, and other data structures are supported by Java programming. The insertion, deletion, and element searching operations are available for every data structure. And Java...

4 minutes read.

How to Create an Object in Java

How to Create an Object in Java An object can be defined as a run time entity that contains the blue printof the class. It means that all the member functions...

5 minutes read.

Java List Implementations

ArrayList and LinkedList are the two implementations of List that are for general-purpose. You'll probably utilise an array list the majority of the time because it's quick and provides constant-time...

3 minutes read.

Java Stringjoiner Class

StringJoiner is a class which is used to construct a sequence of characters which are separated by a delimiter. Optionally, it starts with a provided prefix and ended with the...

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

Sealed Class in Java

What is a Sealed Class in Java? In programming, the two main issues that must be taken into account when creating an application are security and control flow. The use of...

5 minutes read.

Java BufferedWriter

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.