Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method overloading and choosing the most specific type

Tags:

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.

like image 292
AllTooSir Avatar asked Feb 20 '12 13:02

AllTooSir


People also ask

When should method overloading be used?

Overloading is a powerful feature, but you should use it only as needed. Use it when you actually do need multiple methods with different parameters, but the methods do the same thing. That is, don't use overloading if the multiple methods perform different tasks.

Can overloaded methods have different data types?

No, you cannot overload a method based on different return type but same argument type and number in java. same name. different parameters (different type or, different number or both).

How many types of method overloading are there?

There are two types of Polymorphism: Method Overloading and Method Overriding. Method overloading means multiple methods are having the same name but different arguments. Method Overriding means the child class can have a method with the same name as the parent class but with a different implementation.

What is the advantage of method overloading?

Method overloading increases the readability of the program. This provides flexibility to programmers so that they can call the same method for different types of data.


2 Answers

Java uses early binding. The most specific method is chosen at compile time. The most specific method is chosen by number of parameters and type of parameters. Number of parameters is not relevant in this case. This leaves us with the type of parameters.

What type do the parameters have? Both parameters are expressions, using the ternary conditional operator. The question reduces to: What type does the conditional ternary operator return? The type is computed at compile time.

Given are the two expressions:

(10%2==0)? null : new Object(); // A (10%2==0)? null : null; // B 

The rules of type evaluation are listed here. In B it is easy, both terms are exactly the same: null will be returned (whatever type that may be) (JLS: "If the second and third operands have the same type (which may be the null type), then that is the type of the conditional expression."). In A the second term is from a specific class. As this is more specific and null can be substituted for an object of class Object the type of the whole expression is Object (JLS: "If one of the second and third operands is of the null type and the type of the other is a reference type, then the type of the conditional expression is that reference type.").

After the type evaluation of the expressions the method selection is as expected.

The example with if you give is different: You call the methods with objects of two different types. The ternary conditional operator always is evaluated to one type at compile time that fits both terms.

like image 135
Hauke Ingmar Schmidt Avatar answered Oct 14 '22 00:10

Hauke Ingmar Schmidt


JLS 15.25:

The type of a conditional expression is determined as follows:

[...]

  • If one of the second and third operands is of the null type and the type of the other is a reference type, then the type of the conditional expression is that reference type.

[...]

So the type of

10 % 2 == 0 ? null : new Object(); 

is Object.

like image 40
assylias Avatar answered Oct 14 '22 00:10

assylias