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.
// 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)); } } |
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)); } } |
prog.java:8: error: integer number too large: 92374612162
int a = 92374612162;
^
1 error
Recommended Posts:
- Java Integer compareUnsigned() method
- Integer rotateLeft() Method in Java
- Integer intValue() Method in Java
- Integer lowestOneBit() Method in Java
- Integer reverseBytes() Method in Java
- Integer shortValue() Method in Java
- Integer rotateRight() Method in Java
- Integer signum() Method in Java
- Java Integer byteValue() method
- Integer reverse() Method In Java
- Java Integer bitCount() method
- Java Integer compare() method
- Integer highestOneBit() Method in Java
- Integer.numberOfLeadingZeros() Method in Java With Example
- Integer valueOf() Method in Java
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.



