Spaces:
Running
Running
| 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 | |