# Integer sum() Method in Java

The java.lang.Integer.sum() is a built-in method in java that 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 that 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

 `// 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

 `// 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```

Program 3 : Find the sum of integers using a loop

## Java

 `import` `java.io.*;`   `public` `class` `Arrays {` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``int``[] arr = { ``2``, ``4``, ``6``, ``8``, ``10` `};` `        ``int` `sum = ``0``;` `        ``for` `(``int` `i = ``0``; i < arr.length; i++) {` `            ``sum += arr[i];` `        ``}` `        ``System.out.println(``"Sum of array elements is: "` `                           ``+ sum);` `    ``}` `}`

Output

```Sum of array elements is: 30
```