How to return 2 values from a Java method?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Returning multiple values from a method in Java is a common requirement but can be somewhat unintuitive since Java does not support multi-valued returns natively like some other languages such as Python or JavaScript. However, multiple approaches can be used to achieve this. This article explores various techniques to return two values from a Java method along with their technical explanations and examples.
Using an Array or Collection
Arrays
The simplest way to return multiple values is by using an array. This approach involves storing the values within an array and returning it. This method works perfectly for values of the same type.
Example
Lists
You can also make use of Java Collections, such as a List, to return multiple values. This can be especially useful if the number of return values isn't fixed.
Example
Using a Custom Class
Creating a custom class is one of the most recommended ways when you have multiple values of different types or when the values logically belong together.
Example
Using Pair or Tuple Libraries
Java doesn't have a built-in Pair class, but external libraries such as Apache Commons Lang or JavaFX provide these utilities. Optionally, you could use tuples if you're working with libraries like Vavr or Javatuples.
Example Using Apache Commons Lang3
Using Map Interface
The Map interface is a suitable choice when you have pairs of values with clear key-value relationships.
Example
Summary Table
| Method Approach | Use Case | Example Code Complexity | When to Use |
| Array | Same-type values | Simple | When values have the same data type |
| List | Same-type, flexible size | Average | When multiple values of the same type and quantity vary |
| Custom Class | Differently-typed values, logical grouping | Complex | When values logically belong to a single entity |
| Pair/Tuple Libraries | Two values, clear relationship | Medium to Complex | Simplifies code with two associated values |
| Map Interface | Key-value pairs relationship | Medium | When pairs need a key-value logic |
Additional Details
Handling Nulls and Optional Values
When dealing with optional or nullable values, Java's Optional class can be considered. This adds more readability and safety around handling null cases.
Performance Considerations
Using custom objects can introduce object overhead, so when performance is a critical aspect, choosing arrays might be beneficial for primitive types. For complex objects, the performance difference is generally negligible.
Future Language Enhancements
While Java doesn't yet natively support multiple return values, future enhancements, including possible language features in new Java versions, might address this limitation. Always keep an eye on the incorporation of Record classes introduced in Java 14 as they could also provide a structured yet compact way to return multiple values.
Java offers multiple ways to return two or more values from a method, each with specific contexts where it's most applicable. Understanding these different approaches helps in choosing the best solution for your problem, balancing code readability, performance, and functionality.

