R. Clayton (rclayton@monmouth.edu)
(no date)
How can I get a java function to return two values, say a boolean and a
String? In C++ I could return one as the function value and pass the other
as a reference parameter.
The two ways to go about this that pop into mind are to create a return value
and to create a mutable boolean. The return value would be something like
class retval {
String str;
boolean bool;
retval(String s, boolean b) { str = s ; bool = b ; }
}
retval method() {
return new retval("true", false);
}
The mutable boolean, which is similar to your suggestion, would be something
like
class Bool { boolean b; }
String retval(Bool b) {
b.b = false;
return "true";
}
Of the two, the first is preferrable for at least two reasons. First, it
gathers all the return values at the function return, which is where you'd
expect the return values to be, rather than spreading them out between
function return and the argument list (Pascal programmers may object to this,
but then Pascal programs are not particularly readable in this repsect).
Second retval can be made immutable, which is handy when dealing with
concurrency because immutable values can't cause interference. Bool is
necessarily mutable.
This archive was generated by hypermail 2.0b3 on Tue Aug 20 2002 - 12:45:05 EDT