Integer sum() Method in Java Last Updated : 03 Apr, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report 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); } } OutputSum of array elements is: 30 Comment More infoAdvertise with us Next Article Integer sum() Method in Java A ankita_chowrasia Follow Improve Article Tags : Java Java-lang package Java-Functions Java-Integer Practice Tags : Java Similar Reads Integer signum() Method in Java The signum function is an odd mathematical function that extracts the sign of a real number. The signum function is also known as sign function. The Integer.signum() method of java.lang returns the signum function of the specified integer value. For a positive value, a negative value and zero the me 3 min read Integer reverse() Method In Java The java.lang.Integer.reverse() is an inbuilt method in Java and is used to return the reverse order of the bits in the two's complement binary representation of the specified int value. Syntax: public static int reverse(int a) Parameters: The parameter a is an integer value whose bits are to be rev 2 min read Integer shortValue() Method in Java The Integer.shortValue() is an inbuilt method of java.lang which returns the value of this Integer in the short type . Syntax: public short shortValue() Parameters: The method does not take any parameters. Return Value: The method returns the integer value represented by this object after converting 2 min read Java Integer bitCount() Method The bitCount() method of Integer class of java.lang package returns the count of set bits in a positive number. For negative numbers, it returns the count of set bits in its 2s complement form. This function is sometimes referred to as the population count. Syntax:public static int bitCount(int n)Pa 2 min read Integer decode() Method in Java It is often seen that any integer within (" ") is also considered as string, then it is needed to decode that into the integer. The main function of java.lang.Integer.decode() method is to decode a String into an Integer. The method also accepts decimal, hexadecimal, and octal numbers. Syntax : publ 2 min read Like