Integer sum() Method in Java

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Output:

The sum is =80

Program 2: Below program illustrates the exception.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Output:

prog.java:8: error: integer number too large: 92374612162
       int a = 92374612162;
               ^
1 error


My Personal Notes arrow_drop_up

lets make it a lil simple

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.