Skip to content
Related Articles

Related Articles

Improve Article
Integer sum() Method in Java
  • Last Updated : 05 Dec, 2018

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.

Syntax :

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.

Examples



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.




// Java program to illustrate the
// Java.lang.Integer.sum() method
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
        int a = 62;
        int b = 18;
        // It will return the sum of two arguments.
        System.out.println("The sum is =" + Integer.sum(a, b));
    }
}
Output:
The sum is =80

Program 2: Below program illustrates the exception.




// Java program to illustrate the
// Java.lang.Integer.sum() method
import java.lang.*;
  
public class Geeks {
  
    public static void main(String[] args)
    {
        // When very large integer is taken
        int a = 92374612162
        int b = 181;
        // It will return the sum of two arguments.
        System.out.println("The sum is =" + Integer.sum(a, b));
    }
}
Output:
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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :