×

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

How to Convert Timestamp to Date in Java

How to Convert Timestamp to Date in Java You can convert Timestamp to Date by using the constructor of Date class. It returns the long millisecond from Epoch (1st January 1970)...

2 minutes read.

Java ResultSetMetaData

The data about another data is called Metadata. The ResultSetMetaData is used to store the data about ResultSet. The ResultSet Contains the columns, rows, names of table, datatypes etc. these...

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.

Java LDAP Authentication

WHAT IS LDAP? Clients can communicate with directory services by sending requests and receiving responses using the Lightweight Directory Access Protocol (LDAP). The term "LDAP server" refers to a directory service...

7 minutes read.

Java Volatile Keyword

The compiler, runtime, or processors may use any kind of optimization if there aren't any required synchronizations. Although most of the time these improvements are advantageous, they occasionally can result...

6 minutes read.

How to get the current date and time in Java

Introduction: In this article, we are going to discover many processes for Getting the existing-day Date and Time in Java. Most programs require timestamping events or showing date/times, among many...

3 minutes read.

Java Read File Line by line

Java provides two options to read files line by line. Each option offers different benefits and has its own set of drawbacks. Following are the ways through which on accomplish...

5 minutes read.

Java protected vs private

Java : Java is a pure object oriented language. It was introduced by James Gosling in the year 1995. The first public implementation of java was done by sun micro systems...

3 minutes read.

Java Serialization

JAVA SERIALIZATION Serialization is a process by which objects can be represented as a sequence of bytes. These bytes have information about object's data, object's type and datatypes of members in...

3 minutes read.

Java LinkedList

LinkedList Java The Java LinkedList is used to store the elements by using a doubly linked list. It contains the duplicate items, also maintains the order of the insertion, the class...

5 minutes read.

Java Font

The font is a Java class that is a part of java.awt package. The Serializable interface is implemented by it. The direct recognized child of a Java Font class is...

8 minutes read.

RMI program in Java

Remote Method Invocation is what it stands for. An object can call the method of another object in a different space address using the RMI API, which may be on the...

3 minutes read.

Java Print Writer

PrintWriter: It is used to write output data from the file. It is the class of Java.io package. It inherits the properties of the Writer class. Writer Class: It is a class of...

4 minutes read.

How to convert float to String in Java

How to convert float to String in java There are following methods to convert float to String: Convert using Float.toString(float) Convert using String.valueOf(float) Convert using Float.toString(float) The Float class has a static method that returns...

2 minutes read.

Java Exception Propagation

Java Exception Propagation When an exception is being thrown from the peak of the stack and not getting caught, it runs down the stack to the previous method, which is sitting...

3 minutes read.

Java Enum Keyword

Definition: A data type in Java called Enum has a respect to supply of constants. The weekdays (SUN, MON, TUE, WED, THU, FRI, and SAT), directions (NORTH, SOUTH, EAST, and WEST),...

4 minutes read.

Java Developer

Who is a Java Developer? A Java developer is a skilled programmer who works on commercial applications, software, and webpages.  Java developers can work in two different areas: Operating system development:...

3 minutes read.

Java Thread Priority in Multithreading

As we realise, java, being object-situated, works inside a multithreading climate in which the string scheduler relegates the processor to a string in light of the need for a string....

6 minutes read.

Deque in Java

Deque in java collections with Example Deque is short for “double-ended queue.” It is a linear collection that extends the Queue interface and supports insertion and deletion of the element at both the...

3 minutes read.

Minimum Number of Taps to Open to Water a Garden in Java

Problem Statement The issue is that a gardener wants to water a (single-dimensional) garden using the fewest possible tap openings. The goal is to determine the minimum necessary taps to be...

8 minutes read.