11

Sometimes you want to return multiple values from a function. How is this normally done in Java?

One option is to use an array, like this Python snippet that returns a list or tuple:

value, success = read_unreliably()
if success:
    print value

Another option would be to return a hash/dict, like this JavaScript example:

var result = readUnreliably()
if (result.success) {
    alert(value);
}

One more would be to create a custom object just for this purpose, like this Java example:

ReadUnreliablyResult result = readUnreliably()
if (result.getSuccess()) {
    System.out.println(result.getValue());
}

Of course you can also just use some global variables to store what you need instead of passing things around, but let's just say that's not an option.

6 Answers6

26

How is this normally done in Java?

Painfully.

One option is to use an array, like this Python snippet that returns a list or tuple...

Another option would be to return a hash/dict, like this JavaScript example...

These techniques don't work well in Java; the values would have to be downcast from Object.

One more would be to create a custom object just for this purpose, like this Java example...

This is most common, but it is tedious to create all those little classes. In C++ it is common to use std::pair but this is not commonly done in Java.

With Lombok it is quite easy to create little custom objects:

@RequiredArgsConstructor
public class X {
  private final boolean status;
  private final byte[] data;

}

kevin cline
  • 33,798
13

Yes, the way to create a struct / tuple / record in Java is by creating a class. If it's only for internal use, I prefer to use a small immutable struct-like class with public fields.

Examples:

public class Unreliably {
    public final boolean success;
    public final int value;
    public Unreliably(boolean success, int value) {
        this.success = success; this.value = value;
    }
}

This is much easier to do in Scala:

case class Unreliably(success: Boolean, value: Int)
Jonas
  • 14,887
  • 10
  • 70
  • 103
5

For the example you have given, throwing an exception would be the most standard way to handle it:

try {
    result = readUnreliably();
    System.out.println(result);
} (catch IOException e) {
    System.out.println("Could not read file");
}

Moreover, if you strive to have functions that "Do One Thing" the feature of returning multiple values is less necessary, though occasionally convenient.

Eric Wilson
  • 12,111
4

We have a Pair generic class which can store one instance of A and one instance of B. You can probably create a Triplet or Quadruplet in the same manner.

public class Pair<A, B>
{
    ...
}
Raku
  • 1,073
1

In Java, your function would return an object (or perhaps null in the case of a failure). This way, multiple pieces of data can be returned.

1

There is no explicit way to return multiple variables in Java, however there are a few approaches:

  1. The first is to go the way of the array. This only really works if you have everything as the same data type or can temporarily convert them to one type.
  2. The second way is to create a class for the purpose of transferring multiple variable types. At my work we refer to these as DTOs or Data Transfer Objects.