Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2007

Tip: Looking for answers? Try searching our database.

return values of multiple datatype

Thread view: 
Mike - 05 Jun 2007 03:48 GMT
Hi

I am a newbie in java.
By using return in function, we can return a value of some variable or
an array of some special data type.
I am wondering if there are multiple datatype, e.g. String and int,
can I return simultaneously both of them?
Could we declare an array with different datatype?

Thank you.

Mike
phi - 05 Jun 2007 06:54 GMT
Mike schrieb:
> Hi
>
[quoted text clipped - 8 lines]
>
> Mike

Hi

First Question
****************

No, you can not return two types simultaneously. If you want to return
an int and a String simultaneously, you will have to generate a class
(type):

eg:
public class IntStr { int i; String s;}

your funcion might look like this

public IntStr xyz() {
 IntStr result = new IntStr();
 result.i = 42;
 result.s = "Hello Universe";
 return result; }

Second question:
*******************

No as well: an array has all its values the same type. You may specify

Object myArray[];

and then assign any type (Object) to your array. Because every Type is a
subclass of "java.lang.Object" this will work.
But this is probably not what you was looking for ?

phi
Twisted - 05 Jun 2007 07:17 GMT
> public IntStr xyz() {
>   IntStr result = new IntStr();
>   result.i = 42;
>   result.s = "Hello Universe";
>   return result; }

Personally, I'd make IntStr have a suitable constructor so that the
above method body can be written in one line:

return new IntStr(42, "Hello Universe");

And I'd give it a better name.

When you want different kinds of things in an array, usually you are
either really looking to put a common supertype they're specialized
from in the array, or you're looking to have a fixed-size bunch of
different things used differently, which is better implemented as
instances of a class with a bunch of fields.

There are also other ways to return multiple things from a function.

In an either/or case you can (but it's evil) make all but one of them
a exception types, or objects wrapped in a exceptions, and throw them.
Sort them out in the caller with a try and bunch of catch clauses.

Since that's evil, you can also use a "union" as if from C, but it
will take the form of a Java class with a bunch of different fields, a
constructor for each field that sets it to a parameter and the others
to null, and methods to query the fields. The caller finds the non-
null to find out what type was in it.

Another possibility is mutating fields in an object, or the members of
a collection or array, passed as a parameter to the function. This can
lead to unclear code if it isn't carefully documented that the method
modifies the parameter, or if it's done cack-handedly enough. This is
useful if the function has a "return value" that is used immediately
when it returns (just return this) and also needs to communicate
something out-of-band (this uses the mutable collection or whatever).
The Builder pattern also passes as parameters objects that get
modified, sometimes, with methods that perform construction steps on a
data structure.

On the other hand, if a group of values is the logical return value of
the function, rather than a single value, then the function should
return a composite of some kind. An array, collection, or a custom
class with named fields for the components. It may be there's a design
problem whereby the method is trying to do multiple responsibilities
and should be divided and otherwise refactored, also; or it may be
that a logical extension of the way things are developing is for the
return type container object to become a "real" class with its own
behavior beyond querying the fields (or just making them public
final).

Use your design intuition to decide which approach to use in each
individual case where you find yourself wanting multiple returned
objects or composite returns.
Mike - 05 Jun 2007 07:24 GMT
> Mike schrieb:
>
[quoted text clipped - 43 lines]
>
> phi

thank you phi.
Roedy Green - 15 Jun 2007 11:26 GMT
>I am a newbie in java.
>By using return in function, we can return a value of some variable or
>an array of some special data type.
>I am wondering if there are multiple datatype, e.g. String and int,
>can I return simultaneously both of them?
>Could we declare an array with different datatype?

There are two methods:

1. the official way is to invent a new type with two fields a String
and int with getters and setters. You return one of those. Sun does
this with Point, Dimension and Location.

2. the quick and dirty way:  
return new Object[] { theString, new Integer( theInt )};

then you have cast to get the String back and unbox the Integer to get
the int back out -- automatic in JDK 1.5+.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Roedy Green - 15 Jun 2007 11:47 GMT
>I am a newbie in java.
>By using return in function, we can return a value of some variable or
>an array of some special data type.
>I am wondering if there are multiple datatype, e.g. String and int,
>can I return simultaneously both of them?
>Could we declare an array with different datatype?

I have written two essays on this:

http://mindprod.com/jgloss/return.html
http://mindprod.com/jgloss/multiplereturn.html
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.