×

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

Java Abstraction

Java Abstraction Abstraction is an advanced feature of Java to make it transparent. The main motive behind the abstraction is to deal with ideas, not with events. Abstraction is a process...

4 minutes read.

Java copy constructor Example

Java provides the copy constructor much like C++ does. However, it is produced by default in C++. While we define our own copy constructor in Java. With an example, we will...

3 minutes read.

Java Command Line Argument

Command-line arguments are passed to the main() method when we want to pass information into a program during runtime. It is the information that directly follows the program’s name on the...

1 minute read.

Java Short Keyword

Java supports eight different primitive datatypes. The language has predefined primitive datatypes that are given keyword names. Let's take a closer look at each of the eight primitive data types....

3 minutes read.

Java IO file not found exception

One of the exception classes offered by the java.io package is FileNotFoundException. An exception is raised when we attempt to access a file that isn't present in the system. It...

4 minutes read.

Java 8 Consumer Interface in Java

The Consumer Interface is used to implement the functional programming in Java. The Consumer Interface indicates a function that accepts a single input and outputs a result. These functions don’t...

2 minutes read.

Perfect Number in Java

The concept of a perfect number in Java will be defined in this chapter, along with creating Program code that determine whether a specific number is perfect or not. Additionally,...

4 minutes read.

Misc Operators in Java

In this article, we are going to learn about the misc operators in Java. Misc operators are nothing but the miscellaneous operators. The java programming language supports some of the...

4 minutes read.

IdentityHashMap in Java

The IdentityHashMap class is comparable to the HashMap class and is an AbstractMap implementation. However, when comparing the key, it uses reference equality rather than object equality (or values). Identity HashMap...

6 minutes read.

Java Thread Lifecycle: States and Stages

Multithreading Multithreading is one of the most important features in object-oriented programming, and this multithreading can be implemented by various object-oriented programming languages like Java, Python, and C++. A multithreaded process...

7 minutes read.

Int vs Integer in Java

In Java, numerical data can be stored using int or Integer. int and Integer both have different definitions but similar usage in Java. What is int in Java? int is a primitive...

4 minutes read.

Java Integer compareUnsigned() method

The compareUnsigned() method of Integer class compares two int objects numerically by treating the values as unsigned. Syntax public static int compareUnsigned(int x , int y) Parameters The parameters ‘x’ and ‘y’ represent the...

1 minute read.

Java Technologies List

Introducing Java technology is not necessary. Everyone across the globe is still in awe of Java's incredible capabilities for developing mobile apps and websites. Of course, you can be persuaded...

8 minutes read.

How to check valid date in Java?

Every time we get data for any application, we must first ensure that it is accurate before continuing with any further processing. We might have to confirm the following while dealing...

4 minutes read.

Intersection Point of two linked list in Java

In this article, you will be very well acknowledged about how to achieve the intersection point of two linked list in Java. There are several approaches to obtain. Surely each...

8 minutes read.

Enum Java Interview Questions

1. What exactly is Java? Java is a portable, high-level, object-oriented, robust, secure, platform-independent, multi-threaded, high-performance programming language. Developed in June 1991 by James Gosling, also known as Platform. 2. Enum is...

3 minutes read.

Java Destructors

Before knowing about Java destructors, let us know about constructors. If we understand the concept of the constructor, we can easily understand about destructor and also, we will get an...

3 minutes read.

Selection Sort in Java

Selection Sort in Java Selection sort is also a simple sorting algorithm that works by repeatedly finding the minimum element from the unsorted portion of the input array, and placing it...

5 minutes read.

Java calculate age

In this section, we will create a Java program that calculates age from the given date of birth or current date. In order to get the date of birth from the current...

6 minutes read.

Factorial Program in Java using Recursion

Factorial Program in Java Factorials are used in mathematics to calculate permutations and combinations. It is denoted by the exclamatory symbol (!). Suppose, p is a number whose factorial is to...

3 minutes read.