The java.lang.Integer.sum() is a built-in method in java which returns the sum of its arguments. The method adds two integers together as per the + operator.
public static int sum(int a, int b)
Parameter: The method accepts two parameters which are to be added with each other:
a : the first integer value.
b : the second integer value.
Return Value: The method returns the sum of its arguments.
Exception: The method throws an ArithmeticException when the result overflows an int.
Input: a = 170, b = 455 Output: 625 Input: a = 45, b = 45 Output: 90
Below programs illustrate the Java.lang.Integer.sum() method:
Program 1: For a positive number.
The sum is =80
Program 2: Below program illustrates the exception.
prog.java:8: error: integer number too large: 92374612162 int a = 92374612162; ^ 1 error
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.