Linked by Bjorn Raupach on Thu 17th Jul 2008 06:01 UTC
Java Today was one of those days when I wished Java would support multiple return values. I had to develop a rather CPU-intensive algorithm which would compute a solution for a knotty constraint problem. Having a solution alone is sometimes not enough and you also need to add some parameters which measure the quality of the computed outcome. Most of these accompanying parameters can or have to be computed within the algorithm itself, but Java allows you to return only one value either an object or a primitive type. People working with Lisp, MATLAB or Perl, just to mention a few, don't have a problem like this at all. Functions supporting multiple return values is already implemented at the language level and frameworks make heavy use of this. But as a Java programmer you are pretty much stuck here and need to consider some other means to come out of this situation. In the following I would like to give some hints on that topic. Hopefully they are of help for anyone having the same problem every now and then.
Thread beginning with comment 323470
To view parent comment, click here.
To read all comments associated with this story, please click here.
averycfay
Member since:
2005-08-29

but couldn't you just return an array of pointers to the multiple data values you want to return?


1.) Java doesn't have pointers.
2.) There's no type safety in that approach (meaning it's not a good idea for C++ too)

Reply Parent Score: 3

Vanders Member since:
2005-07-06

With C++ you'd just pass multiple values by reference, rather than muck about trying to "return" multiple values.

Reply Parent Score: 2

FunkyELF Member since:
2006-07-26

With C++ you'd just pass multiple values by reference, rather than muck about trying to "return" multiple values.


Then there is no "contract" in the function prototype. It is nice to know by looking at the arguments what can be changed and what can't be changed. You wind up looking at the code to see what is really going on, or actually reading the documentation which nobody wants to do.

Pointers are an ugly hack. The output variable is an input to the function?

Reply Parent Score: 3

tristan Member since:
2006-02-01

"but couldn't you just return an array of pointers to the multiple data values you want to return?


1.) Java doesn't have pointers.
2.) There's no type safety in that approach (meaning it's not a good idea for C++ too)
"

My first thought in C++ would be to have a templated ReturnTriple struct along the lines of:

template<class A, class B, class C>
struct ReturnTriple
{
A first;
B second;
C third;
};

which is cheap, type-safe and reusable.

EDIT: Or, as another poster pointed out, just pass parameters by reference. Is this not possible in Java?

Edited 2008-07-17 17:37 UTC

Reply Parent Score: 2

saucerful Member since:
2008-06-12

Java doesn't have data structs so it would have to be a class but the same concept works.

In fact it makes sense... if you're returning multiple values, they have a relationship. Chances are that that relationship will be studied elsewhere in the code. For instance other methods will take as input both of those pieces of data. Or there will be methods that specifically massage that data. What better place to put them than in this return value class? So in the end such a class almost always pays off by making code easier to read and more organized.

Edited 2008-07-17 18:31 UTC

Reply Parent Score: 2

FunkyELF Member since:
2006-07-26

My first thought in C++ would be to have a templated ReturnTriple struct along the lines of:

template
struct ReturnTriple
{
A first;
B second;
C third;
};

which is cheap, type-safe and reusable.

The first poster on here gave a nice link for javatuple which does something very similar. I just looked and it seems real nice. it has classes for Single, Pair, Triple etc. up to Decuple.
I'm not sure if your C++ example retains type safety but this javatuple package does.


EDIT: Or, as another poster pointed out, just pass parameters by reference. Is this not possible in Java?


Java has Objects and primitives (because back in the day people were bitching about performance). Primitives are pass by value, Objects are passed as a reference. This means that if you pass a Vector to a method, because it is an Object, if you add or remove from that Vector in the method the changes will be visible after the method is called. However, even though Integer is a class, there are no methods to change the value of one. So, a method that needs to return two integers could return a List<Integer> or an Integer[], but things get ugly when you need to return an Integer and a String.

Reply Parent Score: 2

msundman Member since:
2005-07-06

Java doesn't have pointers.

Sure java has pointers. You just can't manipulate them.
E.g.:
Object a=new Object(), b=a;
Here a and b are pointers to the same object.

Reply Parent Score: 7

tristan Member since:
2006-02-01

"Java doesn't have pointers.

Sure java has pointers. You just can't manipulate them.
E.g.:
Object a=new Object(), b=a;
Here a and b are pointers to the same object.
"

To a C++ programmer, a and b look a lot more like references than pointers...

Reply Parent Score: 1

IvoLimmen Member since:
2005-07-06

1.) Java doesn't have pointers.

Java works with references and a reference is a typed pointer. So if you use Object you kind of are using pointers...
2.) There's no type safety in that approach (meaning it's not a good idea for C++ too)

Exactly: that's why Java made them type-safe.

Reply Parent Score: 2

ebasconp Member since:
2006-05-09

1.) Java doesn't have pointers.


Where do you get that assert from???

Everything in Java but the primitive data types are referred via pointers (ok, let's call them "references", but in Java they are the same).

When you declare

MyObject obj = new MyObject();

you are creating an instance of class MyObject and you are assigning a pointer to that instance.

The only difference here is that you cannot do pointer arithmetic (as in C).

Reply Parent Score: 4

Ralf. Member since:
2005-08-13

1.) Java doesn't have pointers.


If Java does not have pointers, why is there a "NullpointerException"? ;-)

Reply Parent Score: 3