• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Getting results from method arguments???

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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;
}</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.
 
So I left, I came home, and I ate some pie. And then I read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic