• Post Reply Bookmark Topic Watch Topic
  • New Topic

Returning multiple variables from a method  RSS feed

 
John Morgan
Ranch Foreman
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now that I have figured out how to return a single variable from a method I am on to the next step, I need to return 2 or 3 variables from a method. I have searched the internet on how to do this to no avail (am I doing the wrong search? I do not know) but what I need to do is return a second (and eventually a third variable)

Here is what I have



Which does not work. Suggestions?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't. Think about it: does that really make sense?

If you have multiple related values that should be treated as a unit, define a class to encompass them. You can return an instance of that class.
 
John Morgan
Ranch Foreman
Posts: 78
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:You can't. Think about it: does that really make sense?

If you have multiple related values that should be treated as a unit, define a class to encompass them. You can return an instance of that class.


That is kind of what I thought (I was kind of hoping that I was just missing something). I guess it is time to go back to class... I mean learning how to turn these into classes. Thanks for the information.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Bear has noted, the only possible way to achieve such a goal in java is to design a class encompassing the information treating them as a single unit...

Something along the lines of:
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd advise creating a simple Engine class that contains properties for that Engine.


 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote:Something along the lines of:

I really don't know what you re trying to do here.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:You can't. Think about it: does that really make sense?


I wouldn't say that the concept of returning multiple variables doesn't make sense, its just something that Java doesn't allow. Other languages do allow it, so maybe OP has experience of those? I'm working on a C++ project that also uses Python at the minute, and both of those support the concept of Tuples. I will probably miss the ability to do that when I go back to Java.
 
Jude Niroshan
Ranch Hand
Posts: 132
5
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I think the best way to return multiple values is just create an object and set those final values which you want to return from the method. That makes more sense. As far as i know.

If you still think that it can waste unnecessary memory, then try to include the Inversion Of Control / dynamic objects.

Regards,
Jude
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:
Bear Bibeault wrote:You can't. Think about it: does that really make sense?


I wouldn't say that the concept of returning multiple variables doesn't make sense, its just something that Java doesn't allow. Other languages do allow it, so maybe OP has experience of those? I'm working on a C++ project that also uses Python at the minute, and both of those support the concept of Tuples. I will probably miss the ability to do that when I go back to Java.


I moved from C++ to Java and have never missed them.
I never did like arbitrary stuff being returned by a method.

On the other hand, I started in Ada and I did miss the whole IN, OUT and IN/OUT parameters for a while.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:I wouldn't say that the concept of returning multiple variables doesn't make sense, its just something that Java doesn't allow. Other languages do allow it, so maybe OP has experience of those? I'm working on a C++ project that also uses Python at the minute, and both of those support the concept of Tuples.

But wouldn't a Tuple be an object of some kind? What C++ has, since it's an extension of C, is a struct, which sounds a bit closer to what John wants.

However, in my experience, a function that needs to return multiple values almost always has a reason for doing so; and that's usually because they represent either:
1. An array or collection of some sort.
2. Some other object used in the system.

@John: That said, I have come across rare cases where the returned values simply represent a "result"; and in those cases I usually define a nested XyzResult class (often private).
Another advantage of defining a custom class to hold these values is that you can document it. I don't know, but I suspect that might be more difficult to do with a generic 'Tuple'.

HIH

Winston
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:On the other hand, I started in Ada and I did miss the whole IN, OUT and IN/OUT parameters for a while.

I was a Progress programmer for quite a while, and it has the same idea. Didn't know it came from Ada though.

Winston
 
Dave Tolls
Ranch Foreman
Posts: 3061
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Dave Tolls wrote:On the other hand, I started in Ada and I did miss the whole IN, OUT and IN/OUT parameters for a while.

I was a Progress programmer for quite a while, and it has the same idea. Didn't know it came from Ada though.

Winston


You'll see it in PL/SQL for Oracle as well, since they nicked the whole structure of Ada for it, including the PACKAGE spec and body concept.
Which did help when I did a year or two in PL/SQL programming.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
But wouldn't a Tuple be an object of some kind? What C++ has, since it's an extension of C, is a struct, which sounds a bit closer to what John wants.


It probably is an object (it is in c++, and probably is in python). But its still returning multiple values. And the syntax around them allows the values to be placed into multiple variables from one function call (more built-in in python, but emulated well in c++). And objects are just struck with function pointers anyway ;)

Winston Gutkowski wrote:
However, in my experience, a function that needs to return multiple values almost always has a reason for doing so; and that's usually because they represent either:
1. An array or collection of some sort.
2. Some other object used in the system.


I certainly wouldn't use a tuple as an array, they are not at all good at that. Where I find them useful is when I need to return loosely coupled things. If I created a class to do it, the class would contain nothing but those items and the getters to retrieve them. It is much more convenient to use a tuple. Documentation is still possible, but I often find third party libraries have terrible documentation with or without tuples. It's a skill that needs promoting.

Anyway, my point wasn't so much that tuples are great. Just that the concept of returning multiple values is not inherently wrong and it makes sense that some people using java for the first time might expect to he able to do it.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose that you have a method and you want it to return two values instead of one.

You could do that by returning an array, which is an ugly way to solve it. Especially if the two values you want to return are of different types, this is going to be very ugly.

Why is this horrible? Because the caller is forced to cast the elements of the array, and from just the method signature it's impossible to know what this method returns. Without looking at the source you don't know how many elements the returned array contains and what types of objects it contains. It's a very user-unfriendly method.

Slightly better, but still not a great way to do this is to make a generic Pair class and return that.

This is slightly better: you know that this method always returns two values, and their types are Integer and String. But this solution is still not great. Why not? Because you have no idea what the two values in the pair represent. What is the meaning of the Integer and the String in the pair? In the pair they are just called "first" and "second", from those names you don't know what these values mean.

It is much better to create a class specifically for the two return values. Give the class and the member variables meaningful names.

For someone who is calling this method, it's now immediately clear what the returned values mean.


Note that tuples, which some of the others in this topic are talking about, are almost exactly like the second of my three solutions, with the same drawbacks: the tuple values don't have meaningful names.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:You could do that by returning an array, which is an ugly way to solve it...

I agree with almost everything you say, with one exception: If the objects are similar, and the method name makes it clear what's being returned, then I'd say it's an acceptable thing to do.

Just one example is:
public BigInteger[] divideAndRemainder(BigInteger divisor) {

And another might be:
public int[] getCoordinates() {

especially if the number of dimensions isn't known at compile time.

My 2¢.

Winston
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is probably a useful debate to be had here about the usefulness of Tuples and multiple returns, but I hesitate to take it further because I don't want to confuse the OP further. I think the discussion would be better in a non-java specific forum though, because I agree with what was said above. I can't think of a way to write a useful tuple in Java. Writing dedicated objects is definitely the way to go in general.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!