Java Integer sum() Method

The `Integer.sum()` method in Java is a static method used to return the sum of two `int` values.

1. Introduction
2. `sum()` Method Syntax
3. Examples
• Summing Two Positive Integers
• Summing a Positive and a Negative Integer
• Summing Two Negative Integers
4. Real-World Use Case
5. Conclusion

Introduction

The `Integer.sum()` method is a convenient way to add two `int` values. This method is part of the `Integer` class and provides a straightforward way to perform addition.

sum()() Method Syntax

The syntax for the `Integer.sum()` method is as follows:

``````public static int sum(int a, int b)
``````
• a: The first integer to be summed.
• b: The second integer to be summed.

The method returns:

• The sum of the two specified integers.

Examples

Summing Two Positive Integers

The `sum()` method can be used to add two positive integers.

Example

``````public class SumExample {
public static void main(String[] args) {
int a = 10;
int b = 20;
int result = Integer.sum(a, b);

System.out.println("Sum of " + a + " and " + b + ": " + result);
}
}
``````

Output:

``````Sum of 10 and 20: 30
``````

In this example, the method adds the integers `10` and `20`, resulting in `30`.

Summing a Positive and a Negative Integer

The `sum()` method can also be used to add a positive integer and a negative integer.

Example

``````public class SumPositiveNegativeExample {
public static void main(String[] args) {
int a = 15;
int b = -5;
int result = Integer.sum(a, b);

System.out.println("Sum of " + a + " and " + b + ": " + result);
}
}
``````

Output:

``````Sum of 15 and -5: 10
``````

In this example, the method adds the integers `15` and `-5`, resulting in `10`.

Summing Two Negative Integers

The `sum()` method can also be used to add two negative integers.

Example

``````public class SumNegativeExample {
public static void main(String[] args) {
int a = -8;
int b = -12;
int result = Integer.sum(a, b);

System.out.println("Sum of " + a + " and " + b + ": " + result);
}
}
``````

Output:

``````Sum of -8 and -12: -20
``````

In this example, the method adds the integers `-8` and `-12`, resulting in `-20`.

Real-World Use Case

Calculating Total Score

In a real-world application, you might use the `Integer.sum()` method to calculate the total score of a game or competition.

Example

``````public class TotalScoreExample {
public static void main(String[] args) {
int score1 = 50;
int score2 = 75;
int totalScore = Integer.sum(score1, score2);

System.out.println("Total score: " + totalScore);
}
}
``````

Output:

``````Total score: 125
``````

In this example, the method adds the scores `50` and `75`, resulting in a total score of `125`.

Conclusion

The `Integer.sum()` method in Java is a simple and effective way to add two `int` values. By understanding how to use this method, you can efficiently handle tasks that involve summing integers in your Java applications. Whether you are adding positive, negative, or a mix of both types of integers, the `sum()` method provides a reliable solution for these tasks.