wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Getting results from method arguments??? 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 » Beginning Java
Bookmark "Getting results from method arguments???" Watch "Getting results from method arguments???" New topic
Author

Getting results from method arguments???

Josh Lam
Greenhorn

Joined: Feb 16, 2000
Posts: 19
In the C/C++ world, one can get results back from function arguments so I can do the following in C++:
void func(int& x, float* y) {
x = 1000;
*y = 1.22;
}
In Java, there does not seem to be any straight forward way of assigning values to primitive types and returning them via method arguments (kinda unfortunate that Integer, Float objects are immutable).
Probaby one way of doing this is to create a 'helper' object eg
public class Helper {
public int anInt;
public float aFloat;
}
and pass it into the method:
void func(Helper x) {
x.aInt = 1000;
x.aFloat = 1.22;
}

or even:
Helper func() {
Helper x = new Helper();
x.aInt = 1000;
x.aFloat = 1.22;
return x;
}
Is this a common way of doing things or are there any 'standard' ways?
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
That's a fairly common way. It makes the most sense if the values are somehow related in such a way that there is some logical name for the class which contains them. For example, if you want to return the x, y, z coordinates of a point in space, it makes sense to define a class:
<code><pre>class 3DCoordinate {
double x;
double y;
double z;
}</pre></code>
...especially if you anticipate needing to use those variables together in other situations.
You can also use pre-existing container objects such as arrays and Vectors. Your original func(int& x, float* y) example can be mimicked by:
<code><pre> void func(int[] xHolder, float[] yHolder) {
xHolder[0] = 1000;
yHolder[0] = 1.22f;
}</pre></code>
Calling this is a little strange:
<code><pre> int[] X = new int[1];
float[] Y = new float[1];
func(X, Y);
int x = X[0];
float y = Y[0];
</pre></code>
That's just one of many possibilities though - most others are strange too. The best bet is probably to go with the first idea, and extend it by grouping the method func() with the data it operates on, as part of the same class:
<code><pre>class ValueFinder {
public int x;
public float y;
public void func() {
x = 1000;
y = 1.22f;
}
}</pre></code>
Then you get the values later from outside this class like so:
<code><pre> ValueFinder vf = new ValueFinder();
vf.func();
int x = vf.x;
float y = vf.y;
</pre></code>
A more complete example:
<code><pre>class Calculator {
private int a; // an input
private int b; // an input
private int c; // an input

private int x; // an output
private int y; // an output

public void setA(int a) { this.a = a; }
public void setB(int b) { this.b = b; }
public void setC(int c) { this.c = c; }

public int getX() { return x; }
public int getY() { return y; }

public void calculate() {
x = 3 * a + b % 7;
y = a * c - 14;
}
}

public class Test {
public static void main(String[]) {
Calculator calc = new Calculator();
calc.setA(635);
calc.setB(571);
calc.setC(549);
calc.calculate();
int x = calc.getX();
int y = calc.getY();
}
}</pre></code>
This is a fairly typical Java idiom. It may seem like a lot of extra code overhead compared to C/C++, but if the values a, b, c, x, and y are actually related and are used together with any frequency, it can be fairly useful and readable.
Though it's nowhere near as elegant-looking as Perl's lists, or Python's tuples, IMO. But we do what we can with what we have.

"I'm not back." - Bill Harding, Twister
 
jQuery in Action, 2nd edition
 
subject: Getting results from method arguments???
 
Similar Threads
Overloaded functions?
How do I refactor this code
OverLoading Question
Problem in Generic Method Declaration
Why is it wrong to use this() in an overloading method?