The sample code is :
public class OverloadingTest {
public static void test(Object obj){
System.out.println("Object called");
}
public static void test(String obj){
System.out.println("String called");
}
public static void main(String[] args){
test(null);
System.out.println("10%2==0 is "+(10%2==0));
test((10%2==0)?null:new Object());
test((10%2==0)?null:null);
}
And the output is :
String called
10%2==0 is true
Object called
String called
The first call to test(null)
invokes the method with String
argument , which is understandable according to The Java Language Specification
.
1) Can anyone explain me on what basis test()
is invoked in preceding calls ?
2) Again when we put , say a if
condition :
if(10%2==0){
test(null);
}
else
{
test(new Object());
}
It always invokes the method with String
argument .
Will the compiler compute the expression (10%2)
while compiling ? I want to know whether expressions are computed at compile time or run time . Thanks.