The four things about methods that you need to grasp well for the exam are:
- Arguments are always passed by value
- Varargs
- The return value
- Method overloading
Method Arguments
Method arguments are always passed by value, which means that any changes to the values of the arguments exist only within the scope of the method. However, if the argument is a reference to an object, the values of the referenced object’s fields can be changed (if they have proper access level).Take a look at the following code. What’s its output?
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 34 35 36 37 38 39 40 41 42 43 |
public class ByValueExample { void simpleMethod(int i) { // Inside this method "i" refers to a copy of the argument "i" (passed when this method is invoked) // The following change affects the copy, not to the original argument i = 9; // Here the method returns and the copy of "i" is discarded } void simpleMethod(ArrayList<Integer> a) { // Inside this method "a" refers to a copy of the argument "a" (passed when this method is invoked) // Since it's a copy, it's pointing to the same object as the original argument "a" // so invoking the "add" method on the copy of "a" has the same effect that // invoking that method on the original argument a.add(3); // Here we are pointing the copy of "a" to a new created object // but the original argument keeps pointing to the original // object (the ArrayList with 3 elements) a = new ArrayList<>(); // here the method returns and the copy of "a" is discarded } public static void main(String[] args) { ByValueExample b = new ByValueExample(); int i = 10; b.simpleMethod(i); System.out.println(i); ArrayList<Integer> a = new ArrayList<>(); a.add(1); a.add(2); b.simpleMethod(a); System.out.println(a.size()); } } |
Varargs
Varargs is the Java feature that allows invoking a method with an arbitrary number of arguments (0 to n). The syntax is:
parameterType... parameterName
The three things to remember about vargars parameters are:
- There can be only one parameter of this type per method and it’s always the last parameter
- Method can be invoked either with a sequence of arguments or with an array
- Inside the method the argument is always treated like an array
Below there are some varargs examples:
1 2 3 4 5 6 7 8 9 10 11 |
void foo(char c, int... i) { // always the last parameter … System.out.println(i[0]); // inside is treated like an array } … object.foo(‘c’, 1, 2, 3); // invoking with N parameters object.foo(‘c’, new int[] {1, 2, 3}); // invoking with an array |
The Return Value
A method returns to the code that invoked it when it (whichever occurs first):- Completes all the statements in the method
- Reaches a return statement
- Throws an exception
Some things you must know about the return value type:
- When return type is
void
, the method doesn’t return a value but it can contain return; statements for branching - When return type is a class, the object returned must be of the same class or a subclass of that class
- When return type is an interface, the object returned must implement it
Method Overloading
Method overloading is the Java feature that allows a class to have different methods with the same name whenever they have different parameter lists (type or order). The return type is not considered when differentiating methods:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class A { // valid overloading void foo(int i) {} void foo(String s) {} void foo(int i, String s) {} void foo(String s, int i) {} // compiler error: same signature as foo(String s) void foo(String x) {} // compiler error: changing return type doesn’t affect signature int foo(int i) {} } |