multiple return values : Java Glossary

go to home page M words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all) ©1996-2009 2008-04-04 Roedy Green, Canadian Mind Products
multiple return values
You can pass multiple parameters to a method, how do you get multiple values back? Here are four methods:
  1. The official way is to invent a new class with fields for each value with getters and setters for each field. With a decent IDE like IntelliJ Idea, this is fairly painless. You return an object of this new class from your method. Sun uses this technique with Point, Dimension and Location. If you can’t think of a good inclusive English name for this new class, then these two values are not logically related and you should not be writing a method to return multiple fields at once.
  2. the quick and dirty way, you return an array of heterogeneous objects e.g.
    // kludge to return multiple objects from a method.
    
    return new Object[] { aString, new Integer( anInt ) };
    
    // The caller then has to unpack.
    
    Object[] junk = aMethod();
    
    String aString = (String) junk[0];
    
    int anInt = ((Integer) junk[1]).intValue();
    The unboxing intValue() is automatic in JDK 1.5+.

    The two main reasons not to use this technique are:

    • You defeat compile-time type consistency checking. If you use String[] (or similar specific type) instead of Object[] the technique is not quite as disreputable. You might even pass in multiple arrays of different types, but that is getting ugly.
    • It is not at all clear from the code which data goes in which slot. Maintenance programmers will tend to change the assignment and fail to keep the documentation up to date. The code is thus almost as unreadable as machine language.
  3. Pass in an Object as a parameter, and have the callee modify that Object’s fields. You will see Sun doing this with Dimension objects, for example.
  4. Split the work up into several method calls. The first does the computation and the rest retrieve but one of the results each. You can eliminate the computation call by having the result-retriever methods check if the computation is up to date, using lazy evaluation of the results. This is how Sun’s GregorianCalendar works.

CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/multiplereturn.html J:\mindprod\jgloss\multiplereturn.html
CMP logofeedback Please email your feedback for publication, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.191.101]
You are visitor number 12,951.