Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
    
Do "real" java people really go through the trouble of using public getters and setters to hide fields on little internal classes like that? Just curious. –  Chris Farmer Jul 25 '11 at 23:50
2  
Yes, most do. Since many frameworks expect the JavaBean standard, you can regret not having getters and setters. In any case, your IDE will generated them for you. –  Eric Wilson Jul 26 '11 at 2:34
2  
Or you can just leave them out, based on the YAGNI principle. But that doesn't seem to happen as often as one might expect. –  MatrixFrog Jul 26 '11 at 2:46
    
@FarmBoy: I guess I understand for beans and such, but the OP implies that this is just a one-off class designed to return multiple values. –  Chris Farmer Jul 26 '11 at 17:03
    
@ChrisFarmer I guess that with that context, I've never seen a 'real' Java programmer create a class for this purpose. –  Eric Wilson Jul 26 '11 at 18:22
show 1 more comment

6 Answers

up vote 20 down vote accepted

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;

}

share|improve this answer
5  
"How is this normally done in Java?" "Painfully." lol as a Java developer I have to +1 you for that –  TheLQ Jul 26 '11 at 6:16
    
Using a compact immutable struct-like class is not so painful... It is really like writing a C struct... –  Guillaume Jul 26 '11 at 14:34
1  
@Guillaume - sure, but all those little things add up in both time and space. I don't know why the Java community hasn't adopted a standard set of generics like "class Pair<T1,T2> { public T1 first; public T2 second; ... }"; in modern languages you just "return a, b" ; and then write "x,y=f()"; –  kevin cline Jul 26 '11 at 14:55
    
I'm sure some 'ancient' language are able to do it too :) IMHO I found multiple return values can lead to total mess, and if you need to add another return fields, you just have to update your struct like class without to change your method signature. –  Guillaume Jul 26 '11 at 15:24
    
That "Painfully." struck me hard, yes its exactly what I feel as Java developer when I meet similar situations. –  artjom Sep 25 '13 at 10:19
show 4 more comments

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)
share|improve this answer
7  
Actually, in Scala, you'd probably generally just return a Tuple2[Boolean, Int]. Although in this specific example, you'd return an Option[Int]. –  Jörg W Mittag Jul 26 '11 at 0:48
2  
So this answers Chris Farmer's question (in the Q's comments) that real Java people don't necessarily create getters and setters for little internal classes. You are a "real" Java person, right? –  Mike M. Lin Jul 26 '11 at 3:14
2  
In other news, there are no true Scotsmen. –  Joseph Weissman Jul 26 '11 at 4:10
add comment

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>
{
    ...
}
share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.