Thomas Hauck wrote:1. When you pass the overloaded methods a hardcoded String on line 3
tc.method("Test"); // line 3
the "String Version"
will be selected.
The compiler is able to distinguish between String and StringBuffer on line 3.
Both String and StringBuffer reference variables 'sb1' and 's1' can be assigned 'null'.
Here "Test" is a String , hence , it is quite straight forward that the compiler will call the method with String as parameter.
Thomas Hauck wrote:
If on line 4 you attempt
tc.method(null);
you will receive a compiler error.
Question:
Is this because the compiler will not know which method
to select if you attempt to call 2 overloaded methods with null?
If you call the method with null as argument , then both the methods with String and StringBuffer as arguments become eligible to handle the call.
The compiler doesn't find any "specific" method to be called.
Consider the following code
Hope it helps!!