Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Java method arguments be used to return multiple values?

Since arguments sent to a method in Java point to the original data structures in the caller method, did its designers intend for them to used for returning multiple values, as is the norm in other languages like C ?

Or is this a hazardous misuse of Java's general property that variables are pointers ?

like image 821
euphoria83 Avatar asked Apr 07 '09 05:04

euphoria83


People also ask

Can Java method return multiple values?

You can return only one value in Java. If needed you can return multiple values using array or an object.

Can a method return more than one type?

A method cannot return more than one type. The signature of a method contains the return type or void if the method doesn't return anything.

Can a method have multiple arguments?

Multiple ArgumentsYou can actually have your variable arguments along with other arguments. That is, you can pass your method a double, an int, and then a String using varargs. It might seem silly to have multiple arguments in a method that already takes multiple arguments.


2 Answers

A long time ago I had a conversation with Ken Arnold (one time member of the Java team), this would have been at the first Java One conference probably, so 1996. He said that they were thinking of adding multiple return values so you could write something like:

x, y = foo(); 

The recommended way of doing it back then, and now, is to make a class that has multiple data members and return that instead.

Based on that, and other comments made by people who worked on Java, I would say the intent is/was that you return an instance of a class rather than modify the arguments that were passed in.

This is common practice (as is the desire by C programmers to modify the arguments... eventually they see the Java way of doing it usually. Just think of it as returning a struct. :-)

(Edit based on the following comment)

I am reading a file and generating two arrays, of type String and int from it, picking one element for both from each line. I want to return both of them to any function which calls it which a file to split this way.

I think, if I am understanding you correctly, tht I would probably do soemthing like this:

// could go with the Pair idea from another post, but I personally don't like that way class Line {     // would use appropriate names     private final int intVal;     private final String stringVal;      public Line(final int iVal, final String sVal)     {         intVal    = iVal;         stringVal = sVal;     }      public int getIntVal()     {         return (intVal);     }      public String getStringVal()     {         return (stringVal);     }      // equals/hashCode/etc... as appropriate } 

and then have your method like this:

public void foo(final File file, final List<Line> lines) {     // add to the List. } 

and then call it like this:

{     final List<Line> lines;      lines = new ArrayList<Line>();     foo(file, lines); } 
like image 189
TofuBeer Avatar answered Sep 16 '22 17:09

TofuBeer


In my opinion, if we're talking about a public method, you should create a separate class representing a return value. When you have a separate class:

  • it serves as an abstraction (i.e. a Point class instead of array of two longs)
  • each field has a name
  • can be made immutable
  • makes evolution of API much easier (i.e. what about returning 3 instead of 2 values, changing type of some field etc.)

I would always opt for returning a new instance, instead of actually modifying a value passed in. It seems much clearer to me and favors immutability.

On the other hand, if it is an internal method, I guess any of the following might be used:

  • an array (new Object[] { "str", longValue })
  • a list (Arrays.asList(...) returns immutable list)
  • pair/tuple class, such as this
  • static inner class, with public fields

Still, I would prefer the last option, equipped with a suitable constructor. That is especially true if you find yourself returning the same tuple from more than one place.

like image 21
javashlook Avatar answered Sep 20 '22 17:09

javashlook