+ 2

Can i return data more than one in method return? And how to use it?

6th Feb 2018, 2:28 PM
Dimas Chandra
Dimas Chandra - avatar
6 Answers
+ 14
in this example , the return statement will only execute once per method call
6th Feb 2018, 3:12 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 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. 😉
6th Feb 2018, 3:07 PM
Zephyr Koo
Zephyr Koo - avatar
+ 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. 😄
6th Feb 2018, 2:42 PM
Zephyr Koo
Zephyr Koo - avatar
+ 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
6th Feb 2018, 3:03 PM
Dimas Chandra
Dimas Chandra - avatar
+ 3
Thanks for your answers :)
6th Feb 2018, 3:26 PM
Dimas Chandra
Dimas Chandra - avatar
+ 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.
6th Feb 2018, 2:35 PM
Toni Isotalo
Toni Isotalo - avatar