This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Multiple value return type in functions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Multiple value return type in functions" Watch "Multiple value return type in functions" New topic
Author

Multiple value return type in functions

Vinayagam Kulandaivel
Ranch Hand

Joined: Nov 26, 2004
Posts: 43
Techies,

How the JavaSE6.0 handle the function that return multiple values at a time.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

I don't think you're going to have to worry about that...

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4222792

State........Closed, will not be fixed
Vinayagam Kulandaivel
Ranch Hand

Joined: Nov 26, 2004
Posts: 43
Thanks for your ref.
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

You're very welcome!
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
If you want to return more than one value from a method, a Collection class can be constructed and that can be returned to the called place.

But is it a good way of designing ?


Thanks & regards, Srini
MCP, SCJP-1.4, NCFM (Financial Markets), Oracle 9i - SQL ( 1Z0-007 ), ITIL Certified
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
No.

When you feel the desire to return more than one value from a method, that typically means that you are missing an abstraction.

Do you have an example we could discuss?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

When you feel the desire to return more than one value from a method, that typically means that you are missing an abstraction.
I agree; the multiple values you're wanting to return are typically related in some way. These data beg to be encapsulated in a class. If the data are unrelated, they probably shouldn't be returned from the same method...
Srinivasa Raghavan
Ranch Hand

Joined: Sep 28, 2004
Posts: 1228
Ilja,
I fully agree with you. But once i have faced a situation like this. A business object needs the information of an employee's , login time , supervisors Id , the machine in which he logged in, and some extra stuff like initial petty cash amount he had in cash draw while he logged in.

In this situation, a class with the above members was created , and this was returned to the bo.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Maybe a decade ago we requested mainframe data and could not predict what combination of values might come back. Given a customer number we might get any or all of product detail, other products the customer owns, people related to the customer, etc. This was definitely a design compromise ... we distributed logic to the mainframe and smashed all this stuff together because one large payload across the network was faster than three or four small ones. Anyhow, I could see that being a collection and instanceOf checks. I won't argue that it wasn't a design smell.
[ July 27, 2005: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Stan James:
Anyhow, I could see that being a collection and instanceOf checks. I won't argue that it wasn't a design smell.


An lot of early CORBA systems, following the lead of the famous DISCUS case study, were built this way. Everything was of type "Data:" return values, parameters, everything. Data had named properties, and that was about it. Basically everything was just a bag of bits. I worked on a couple of systems like this. It was bloody awful.


[Jess in Action][AskingGoodQuestions]
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
I want to return two ints, therefore I write an appropriate abstraction for two ints:


...and my method:

...TwoInts is probably a silly abstraction in a typical context, therefore, try to define your abstraction. Wanting to "return two values" is always indicative of a failure to abstract appropriately. In my given example, I didn't "return two values", I returned a single value of type "TwoInts" - a mere abstraction of two int values.


Tony Morris
Java Q&A (FAQ, Trivia)
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
With all due respect Tony, I think "TwoInts" is a very weak "abstraction".

I was more thinking along the lines of returning a Point or something - a "real" abstraction with a specific meaning in the solution domain (preferably even in the problem domain).

And returning an object that contains two integers is just *one* of the possible solutions. Another one is to move the method to a different (new?) class, so that it doesn't need to return two ints anymore.

I think this would be much more enlightening if could discuss a real world example. Anyone has one?
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608

With all due respect Tony, I think "TwoInts" is a very weak "abstraction".

Agreed entirely. In fact, it is almost certainly an inappropriate abstraction. Quite often, thinking in terms of that abstraction (as opposed to "returning multiple values") will yield something more tangible anyway.
I didn't mean to portray TwoInts as a solution; merely a demonstration.
Vu Pham
Ranch Hand

Joined: May 28, 2005
Posts: 100
When you feel the desire to return more than one value from a method, that typically means that you are missing an abstraction.


I can't understand why we missed an abstraction here . May you explain me more ?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Anh Vu:


I can't understand why we missed an abstraction here . May you explain me more ?


Let me take Tony's example a step further - he went for something simple and maybe it was too simple to understand the point properly.

Let's make up a fictitious application - let's say we're making a program that plots the locations of tornadoes on a map. In our application, we have a Map class that invokes a method on our DataGatherer class. That method is responsible for returning the latitude and longitude of the most recent tornado.

We might want to do something like this:



Obviously, that's not valid Java code - Java has no means of returning multiple return values. I just made up some syntax to illustrate what we might want to do. We want our getLatestTornado method to return two values - the latitude and longitude of the latest tornado.

However, this is where we can see that we're missing a layer of abstraction. The latitude and longitude of the latest tornado are most certainly related to one another - let's encapsulate those in a class of their own:



Now, if we rewrite our original code using valid Java and the TornadoLocation class, we'd get something like this:



You can see that, by adding the abstraction, TornadoLocation, we've avoided the need to return multiple values from a single method.

While this is a slightly more elaborate example than Tony gave above, it's essentially the same thing and one could probably say that it is still quite contrived. Nonetheless, I hope it helps illustrate what people have been discussing.


SCJP Tipline, etc.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Multiple value return type in functions
 
Similar Threads
callable statement
multiple-entity finder method return type
Is it possible to return multiple values from methods
return statement practices
SQL Results