java.lang.StrictMath addExact(int x, int y)
Description
The addExact(int x, int y) method of StrictMath class returns the sum of its arguments, throwing an exception if the result overflows an int.
Notes:
The addExact(int x, int y) method of StrictMath class is static thus it should be accessed statically which means the we would be calling this method in this format:
StrictMath.addExact(int x, int y)
Non static method is usually called by just declaring method_name(argument) however in this case since the method is static, it should be called by appending the class name as suffix. We will be encountering a compilation problem if we call the java compare method non statically.
Method Syntax
public static int addExact(int x, int y)
Method Argument
Data Type | Parameter | Description |
---|---|---|
int | x | the first value |
int | y | the second value |
Method Returns
The addExact(int x, int y) method returns the sum of the arguments.
Compatibility
Requires Java 1.8 and up
Java StrictMath addExact(int x, int y) Example
Below is a java code demonstrates the use of addExact(int x, int y) method of StrictMath class.
package com.javatutorialhq.java.examples; import java.util.Scanner; /* * A java example source code to demonstrate * the use of addExact(int x, int y) * method of Math class */ public class StrictMathAddExactIntExample { public static void main(String[] args) { // Ask user input System.out.print("Enter a value:"); // declare the scanner object Scanner scan = new Scanner(System.in); // use scanner to get the user input and store it to a variable int intValue1 = scan.nextInt(); // Ask another user input System.out.print("Enter another value:"); // store it to a variable int intValue2 = scan.nextInt(); // close the scanner object scan.close(); // get the result int result = StrictMath.addExact(intValue1, intValue2); // print the result System.out.println("result: "+result); } }