Click to See Complete Forum and Search --> : how to return 2 value?


zhcai
February 23rd, 2000, 04:20 PM
in C++, I can do it by:

void getParam(int *width, int *height);
int width, height;
getParam(width, height);



but how can I do it in JAVA because JAVA does not accept point?

Jan Meeuwesen
February 24th, 2000, 02:25 AM
hi there,

Could you explain more specific what you want (and not how to do it in C++) ?

Greets,
Jan Meeuwesen.

kannanbalu
February 24th, 2000, 02:15 PM
Well,
In Java, all objects are passed by reference.
So any modifications that you make to the passed
arguments will be reflected back as in C++.

Thanks.

mjpell
March 7th, 2000, 01:40 PM
Returning multiple values via a return statement is not allowed in Java. Thankfully.

If you truly wish to get back multiple values, consider creating a new object to hold your values. Often times, at least in business apps, you'll end up adding behavior and/or additional attributes anyway.

If this doesn't seem appropriate in your case, then maybe rethink your design a bit, such that you don't need multiple return values. The entire Java language is written without multiple return values...

good luck,

Jeeves
March 7th, 2000, 03:50 PM
You can either break it up into two methods

int getWidth();
int getHeight();



or return an object with all the information

java.awt.Dimension getSize();

DHunter21
March 7th, 2000, 07:30 PM
Actually if you pass 2 object parameters into the method such as : myMethod(Object a, Object b), then the objects can be modified, since it a reference your passing.

So in terms of you argument, it would look a little more like:


public void getParam(Integer width, Integer height) {
width = new Integer(100);
height = new Integer(100);
}




However this may be more effort than its worth (when you want plain int values).
If you have to send them back at the same time, you could also return a Dimension Object.

Dustin