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 323465
To read all comments associated with this story, please click here.
Never programmed in Java before...
by FishB8 on Thu 17th Jul 2008 17:05 UTC
FishB8
Member since:
2006-01-16

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

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 Bookmark 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 Bookmark Score: 2

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 Bookmark 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 Bookmark Score: 7

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 Bookmark 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 Bookmark 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 Bookmark Score: 3