From Python Cookbook v.30
def myfun():
return 1, 2, 3
a, b, c = myfun()
Although it looks like
myfun()
returns multiple values, atuple
is actually being created. It looks a bit peculiar, but it’s actually the comma that forms a tuple, not the parentheses
So yes, what's going on in Python is an internal transformation from multiple comma separated values to a tuple and vice-versa.
Though there's no equivalent in java you can easily create this behaviour using array
's or some Collection
s like List
s:
private static int[] sumAndRest(int x, int y) {
int[] toReturn = new int[2];
toReturn[0] = x + y;
toReturn[1] = x - y;
return toReturn;
}
Executed in this way:
public static void main(String[] args) {
int[] results = sumAndRest(10, 5);
int sum = results[0];
int rest = results[1];
System.out.println("sum = " + sum + "\nrest = " + rest);
}
result:
sum = 15
rest = 5