Hi there!!
Just wanted to ask please if this is applicable to return multiple values from a method in java as it is in php's functions for instance.
Thanks in advance!!
Atar.
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Hi there!!
Just wanted to ask please if this is applicable to return multiple values from a method in java as it is in php's functions for instance.
Thanks in advance!!
Atar.
How would you do this in php? As far as I know, and unless the php docs are out of date, you cannot
PHP: Returning values - Manual
In java you could effectively do this just as you would in php - by wrapping the values in something else, for instance an array, a Collection, or wrap the values within a particular object.A function can not return multiple values
Please elaborate...
What are you trying to do? I'm sure there is a way, I just don't know what you're asking.
@copeg
Thank you about your quick response!!
Can you post here a little example about how to do so with arrays, collections that you spoke about?
Your help is appreciated!!
Atar.
Last edited by atar; July 31st, 2012 at 09:56 AM.
What copeg said is something like this.Imagine that you want to return 5 values,e.g. 0,1,2,3,4 then you would write something like this
static void f(int[] a) { for(int i=0 ; i<a.length ; i++) a[i]=i; } public static void main(String[] args) { int []a = new int[5]; f(a); }
If you do not understand something ask
@Samaras
All right, but how should I return those multiple values? There's not any reference to that in your code.
Now array a holds these values.So you have access to them via array a.Try writing in main after f(a)
This should output 0,1,2,3,4.So you have access in these values that were created inside function f.Isn't that what you wanted,or i understood wrongly? :/for (int i = 0; i < a.length; i++) { System.out.println(a[i]);
@Samaras: that's not necessarily returning a value, you're simply modifying one. I know it's common practice in C, but there are a few caveats with this method.
The first is you can't actually change what array you have, so the array you're passed is the array you have to modify.
For example:
When you run this code, you'll notice that the program prints out five 0's, not five 3's.
The better method is to instantiate and return said array:
Note that this code still only returns one value: it's returning an array. However, an array is composed of multiple elements, which can be extracted.
Now if you have to return a bunch of values who's type do not correlate, the best practice is to create a class which holds all of the necessary components. For example, take the following class:
public class Customer { public String name; public int id; public double pay; }
Using a similar method as above, you can create and instantiate a Customer object and return it. Again, technically you're only returning one value: a customer object. However, inside that object there is a name, id, and pay fields.
Ok if you return the array or pass it as an argument does not make much difference at this point i would say.
However the example you gave me shocked me a bit.If i comment this line var = new int[5]; ,then the output will be 3's and no 0's.If i uncomment it the output is going to be as you said.WHY?
What if the called method wants to set the size of the array it returns?if you return the array or pass it as an argument does not make much difference
If you don't understand my answer, don't ignore it, ask a question.
.I run it and i will say that if passed as an argument the size won't change.If it is been returned the size is going to change.WHY?
In java i have been told that the arguments are passed as a reference..Is that true?And somehow is this link to this somehow?
Remember i am a BEGINNER˛
There might not be an array passed to the method. It all depends on what the method is supposed to do.If it is been returned the size is going to change.
I would change that to say the size of the returned array would be set by the called method.
Args are passed by value.
If you don't understand my answer, don't ignore it, ask a question.
ok i agree,but my question was not answered yet i think :/
Start your own thread if you have questions. This one was started by atar.
If you don't understand my answer, don't ignore it, ask a question.