Java Long sum() Method

The Long.sum() method in Java is a static method used to add two long values together.

Table of Contents

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

Introduction

The Long.sum() method is a static method in the Long class in Java. It provides a convenient way to add two long values together. This method is useful for performing arithmetic operations involving long values.

sum()() Method Syntax

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

public static long sum(long a, long b)
  • a: The first long value to be summed.
  • b: The second long value to be summed.

The method returns:

  • The sum of the two specified long values.

Examples

Summing Two Positive Longs

The sum() method can be used to add two positive long values.

Example

public class SumExample {
    public static void main(String[] args) {
        long a = 123456789L;
        long b = 987654321L;
        long result = Long.sum(a, b);

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

Output:

Sum of 123456789 and 987654321: 1111111110

In this example, the method adds the long values 123456789L and 987654321L, resulting in 1111111110L.

Summing a Positive and a Negative Long

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

Example

public class SumPositiveNegativeExample {
    public static void main(String[] args) {
        long a = 1000L;
        long b = -500L;
        long result = Long.sum(a, b);

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

Output:

Sum of 1000 and -500: 500

In this example, the method adds the long values 1000L and -500L, resulting in 500L.

Summing Two Negative Longs

The sum() method can also be used to add two negative long values.

Example

public class SumNegativeExample {
    public static void main(String[] args) {
        long a = -1000L;
        long b = -2000L;
        long result = Long.sum(a, b);

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

Output:

Sum of -1000 and -2000: -3000

In this example, the method adds the long values -1000L and -2000L, resulting in -3000L.

Real-World Use Case

Calculating Total Distance

In a real-world application, you might use the Long.sum() method to calculate the total distance covered by multiple trips, where distances are represented as long values.

Example

public class TotalDistanceExample {
    public static void main(String[] args) {
        long trip1Distance = 150000L; // Distance in meters
        long trip2Distance = 200000L; // Distance in meters
        long trip3Distance = 300000L; // Distance in meters

        long totalDistance = Long.sum(trip1Distance, Long.sum(trip2Distance, trip3Distance));

        System.out.println("Total distance covered: " + totalDistance + " meters");
    }
}

Output:

Total distance covered: 650000 meters

In this example, the method calculates the total distance covered by summing the distances of three trips.

Conclusion

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

Comments