In the C++ language, functions (similar to Java's methods) can "return" multiple values of differing data types through the use of reference parameters. C++ reference parameters appear in a function's signature preceded by an amperesand (&). A C++ reference variable points to the same address as the argument variable corresponding to it when the function was called. Thus, when a value in a function of a reference variable changed, so did the value of the calling variable ... because both variables pointed to (referred to) the same location in memory. Thus multiple values could be effectively "passed back" to the calling variables by declaring the function parameters to be reference parameters. Thus C++ reference parameters were often also referred to as "two way parameters" because the passing of values effectively occured both when values were passed from arguments to parameters when a function (like a Java method) was called and also when the value of a reference parameter was modified in the called function. (Remember that the reference parameter and the invoking argument both pointed to the same location in memory.
On 11 October 2007 a student studying Java who had also programmed in languages other than Java asked me if you can return multiple values in Java. I considered the question. I correctly replied that Java only passed "by value" when invoking methods, that Java did not suppport reference parameters. I then incorrectly concluded that Java methods could therefore not return multiple values. The student muttered something like, "Oh, that sucks!". The question bothered me because of the power I had seen when C++ functions used reference parameters to effectively "return" values of multiple variables. Then, a couple days later while doing something completely different (Isn't that always the way?), it struck me that Java is an OOP language and thus Java methods do return values of multiple variables by returning objects!!!
In Java, a class may have as many instance variables as the programmer wishes. Every object declared by that class thus has a corresponding set of values for those instance variables. Since a Java method may return an object, it returns a separate value for each and every instance variable of that object! In this way, in a most elegant manner one might add, Java methods are able to return values of multiple variables.
The following Java program exemplifies how a Java method may return values of multiple variables by return a single object containing values for each of that objects instance variables!"
I created an inner class called Person consisting only of instance variables and no methods. It is thus functionally equivalent to Pascal Record or C language struct. All objects of the Person class will have a value for each instance variable, even if that value is 0 or null.
I then created a method called friendRecord( ) that returns an object of type Person. When a Person object is returned, that object contains a value for every instance variable declared in the Person class.
In my main( ) method, I created an object of type Person called neighbour and assigned it the values returned by the returning Person object when the friendRecord( ) method was called. In this way, the values of the instance fields of the neighbour object were effectively copied from the instance fields of the friend object created in the friendRecord( ) method. Thus I effecitively returned multiple values from a Java method by returning object containing values of multiple instance fields.
I demonstrated the successful return of values from the friendRecord( ) method by printing those very values in my main method following the invocation (calling) of the friendRecord( ) method. See the screen dump following the source code.
public class UsePerson
{
public static class Person
{
String firstName = null;
String lastName = null;
int age = 0;
double income = 0.0;
boolean canVote = true;
}
public static void main( String[] args )
{
UsePerson me = new UsePerson( );
Person neighbour = new Person( ); // neighbour is declared an object of type Person
neighbour = me.friendRecord( ); // friendRecord( ) returns an object of type Person
System.out.print( neighbour.firstName + " " +
neighbour.lastName + " " +
neighbour.age + " " +
neighbour.income + " " );
if( neighbour.canVote )
System.out.println( neighbour.firstName + " can vote." );
else
System.out.println( neighbour.firstName + " can not vote." );
System.exit( 0 );
}
public Person friendRecord( )
{
Person friend = new Person( );
friend.firstName = "Gerry";
friend.lastName = "Donaldson";
friend.age = 39;
friend.income = 50123.57;
friend.canVote = true;
return ( friend ); // returns friend == an object of type Person
}
}
/* Screen Dump
Gerry Donaldson 39 50123.57 Gerry can vote.
*/
|