• Post Reply Bookmark Topic Watch Topic
  • New Topic

Getting results from method arguments???

Josh Lam
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
...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;
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];
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;
Then you get the values later from outside this class like so:
<code><pre> ValueFinder vf = new ValueFinder();
int x = vf.x;
float y = vf.y;
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();
int x = calc.getX();
int y = calc.getY();
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.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!