+ 2
Can i return data more than one in method return? And how to use it?
6 Answers
+ 14
in this example , the return statement will only execute once per method call
+ 10
The example you've given is a conditional return and it will return either a or b, but not both.
Although you may write multiple return like this:-
return a;
return b;
only the value a will be returned successfully as the return statement will return back to its caller immediately, in this case, the main or your program entry point. đ
+ 8
Short answer is no as you can refer to only one object per return statement.
However if the multiple pieces of info required are related, you may consider to group it into a single object and return altogether at once. đ
+ 3
And how about it
class Program {
public static void main(String[ ] args) {
int res = max(7, 42);
System.out.println(res);
}
static int max(int a, int b) {
if(a > b) {
return a;
}
else {
return b;
}
}
}
It means that i Just can return a parameter more than one but i cant return some assigned variable data in return method... Am i right? Give me explain.. Im still newbie LOL
+ 3
Thanks for your answers :)
+ 2
You can't use "return" keyword more than one time in a row because that's where your method execution ends.
Sure you can use it multiple times in one method as long as they don't return data at the same time.
All code after returning data will be ignored
Make sure your method have the right return type.