×

Java Math toIntExact() Method

The toIntExact() method of Java Math class returns the int value of the given long argument, throwing an exception if the value overflows an int.

Syntax:

public static int toIntExact (long value)

Parameters:

The parameter ‘value’ represents the long value.

Return Value:

The toIntExact () method returns an int value.

Throws:

The toIntExact () method throws:

ArithmeticException- if the argument overflows an int

Example 1:

public class JavaMathToIntExactExample1 {
    public static void main(String[] args) {
        Long a=-985565437l;
        //returns the int value of the given long argument
        System.out.println("Int value of "+ a+" is "+Math.toIntExact(a));
    }
}

Output:

Int value of -985565437 = -985565437

Example 2:

public class JavaMathToIntExactExample2 {
    public static void main(String[] args) {
        Long a=0l;
        //returns zero, if argument is zero
        System.out.println("Int value of "+ a+" is "+Math.toIntExact(a));
    }
}

Output:

Int value of 0 is 0

Example 3:

public class JavaMathToIntExactExample3 {
    public static void main(String[] args) {
        Long a=Long.MIN_VALUE;
        //throws an exception if the value overflows an int
        System.out.println("Int value of "+ a+" is "+Math.toIntExact(a));
    }
}

Output:

Exception in thread "main" java.lang.ArithmeticException: integer overflow
            at java.lang.Math.toIntExact(Math.java:1011)
            at com.TutorialAndExamples.JavaMathToIntExactExample3.main(JavaMathToIntExact
Example3.java:7)

Example 4:

public class JavaMathToIntExactExample4 {
    public static void main(String[] args) {
        Long a=Long.MIN_VALUE;
        //throws an exception if the value overflows an int
        System.out.println("Int value of "+ a+" is "+Math.toIntExact(a));
    }
}

Output:

Exception in thread "main" java.lang.ArithmeticException: integer overflow
            at java.lang.Math.toIntExact(Math.java:1011)
            at com.TutorialAndExamples.JavaMathToIntExactExample4.main(JavaMathToIntExact
Example4.java:7)

Related Topics

Java Math min() Method

The min() method of Math class returns the smaller of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double min (double a, double...

2 minutes read.

Java Program to Create Set of Pairs Using HashSet

HashSet in Java: HashSet is an assortment in Java that has a place with java.util bundle. It inside utilises HashMap to store components. It is an execution of the Set connection...

11 minutes read.

Java Boolean Keyword

Boolean keyword In java programming language we basically have two types of primitive data types, Boolean and Numeric (integer and floating-point data types). In this article we are going to learn...

4 minutes read.

Java Map Interface

A map is a collection that maps keys to values, with no duplicate keys allowed. The elements in a map are key/value pairs. HashMap: HashMap stores the keys in a...

3 minutes read.

Java Swings

Swing is a Java Foundation Class library and an extension to the Abstract Window Toolkit (AWT) (JFC).As compared to AWT, Swing has significantly better functionality, new components, increased component features,...

9 minutes read.

Ternary Operator in Java

In some cases, the if...else expression in Java can be replaced by a ternary operator. Visit the Java if...else statement first before learning about the ternary operator. Ternary operator in Java A...

3 minutes read.

Java While Loop

A while loop is used to repeatedly execute a set of statements as long as its condition evaluates to true. This loop checks the condition before it starts the execution...

1 minute read.

Thread Program in Java

Thread Program in Java Thread program in Java is the continuation of multithreading program in Java. In this topic, we will learn about the usage of threads, race condition in multithreading,...

8 minutes read.

Difference between Aggregation and Composition in Java

What is Aggregation? Before we start discussing Aggregation, we have to know some general information about our classes in java. Generally we have two members in our classes , the first...

8 minutes read.

Difference between throw and throws in java

This article shows you the core difference between “throw” and “throws”keywords in Java programming language.The throw keyword tells Java you want another part of the code to deal withthe exception,...

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

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.

Java Math sin() Method

The sin() method of Java Math class returns the trigonometric sine of the specified angle. Syntax: public static double sin(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The sin() method...

1 minute 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.

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.

Java Trim

Leading and leaving spaces are removed by the built-in Java String trim() method. Space seems to have the Unicode element of "x0040." Java trim() function looks for all of these...

3 minutes read.

Instanceof operator in Java

To determine whether an object is an instance of the supplied type in Java, use the instanceof operator (class or subclass or interface). Because it compares the instance with type, the...

3 minutes read.

Java Comparator Interface

Java comparator interface is used in a situation when we have to sort an object which does not implement Comparable or do sorting in a different way than the Comparable....

1 minute read.

Java File Extension

A computer file's suffix is called its file extension. It is easily recognized since it appears immediately after a period (.) in the file name. Consider the file Demo.java as an...

3 minutes read.

Finding middle node of a linked list in Java

To find the middle node of a linked list we have various methods in Java. Method 1 In this method two pointers are used, one of which advances quickly, and the other of...

6 minutes read.