Spaces:
Running
Running
File size: 663 Bytes
6f04d37 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
The method can also return a value.
The return is done in the usual way with a return statement.
The type of return value is specified in the method's header line: in practice, the empty return value type void is replaced by the return value type.
For example, a method that returns the sum of two integers (which is also an integer) would look like this:
public static int sum(int num1, int num2) {
return num1 + num2;
}
Example method call:
public static void main(String[] args) {
System.out.println(sum(4, 2));
System.out.println(sum(10, 5 * 4));
int result = sum(5, 15);
System.out.println(result);
}
Program outputs:
6
30
20
|