Curriculum
Course: Java Basic
Login

Curriculum

Java Basic

Java Home

0/1

Java Introduction

0/1

Java Get Started

0/1

Java Syntax

0/1

Java Comments

0/1

Java Type Casting

0/1

Java Operators

0/1

Java Booleans

0/1

Java Switch

0/1

Java Break / Continue

0/1

Java Errors and Exception

0/1
Text lesson

toIntExact()

Example

Convert long values to int:

System.out.println(Math.toIntExact(1500000L));
System.out.println(Math.toIntExact(-32L));
System.out.println(Math.toIntExact(-86345L));
System.out.println(Math.toIntExact(25L));

Definition and Usage

The tointExact() method converts a long value to an int, triggering an exception if the conversion leads to overflow. This prevents inaccuracies that may arise from overflow situations.

Syntax

public static int toIntExact(long x)

Parameter Values

Parameter

Description

x

 Required. An integer to negate.

Technical Details

Returns:

“An integer value representing the contents of a long integer.”

Throws:

ArithmeticException – If the conversion operation results in overflow.

Java Version:

1.8+