This question already has answers here:
How is an overloaded method chosen when a parameter is the literal null value?
(8 answers)
Closed 6 years ago.
public class Test{
public static void abc(String s) {
System.out.println("String");
}
public static void abc(Object s) {
System.out.println("OBject");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
abc(null);
}}
Output-String
I am Beginner in java,I am confused about the output of the above program.
Please explain me the reason of the output.
Early Binding (Binding most specific method at compile time).
When you overload methods, most specific method will be choosen. In your case the order of choosing is String >Object (since null can be of any reference type).
In the hierarchy, String is more specific than Object. Hence string got chosen. In fact Object is the least specific among all the java objects
Here is the JLS for the same
http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.12.2.5
If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.
..... [rules]
Java compiler chooses most specific overloaded method will be selected.It is called as early binding. Here, String extends Object class hence it is more specific. You can refer official Java Language Specification
If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.
The concept you have used is overloading and Object is superclass for all the classes in java. So when you provide any specific implementation (in this case its String) along with general implementation (in this case its Object) then JVM goes with specific implementation by default.
If you want to try it out please replace abc(null); with abc(123);
In this case, output will be "OBject" as JVM can not find any specific implementation for integer so it goes with generalized one.
Related
This question already has answers here:
Which overload will get selected for null in Java?
(3 answers)
Closed 8 years ago.
I have this confusing code:
public class Confusing {
private Confusing(Object o){
System.out.println("Object");
}
private Confusing(double[]dArray){
System.out.println("double array");
}
public static void main(String[] args){
new Confusing(null);
}
}
When "compiled" and run the program displays "double array" why arrays precede Object? Is there any other constructor situation where such confusing behavior will occur?
A double[] is an Object too, and when choosing which constructor (or method), Java will choose the one with the most specific parameter type that still matches. Because null will match any parameter type, the most specific type matches, and that is double[] here.
It's not confusing once you know this rule, but this will occur when two or more overloaded constructors (or two or more overloaded methods) differ only in that one of the parameter types in one of them is a subclass of the corresponding parameter in another.
The JLS, Section 15.12.2.5 states how Java chooses the method/constructor to invoke when multiple overloaded methods/constructors match:
The Java programming language uses the rule that the most specific method is chosen.
and
The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time error.
Read more in Java Language Specification for choosing the Most specific method rules.
§8.4.9. Overloading
§13.4.23. Method and Constructor Overloading
§15.12.2.5. Choosing the Most Specific Method
This question already has answers here:
Which overload will get selected for null in Java?
(3 answers)
Strange Java null behavior in Method Overloading [duplicate]
(4 answers)
Closed 9 years ago.
In the following program:
public class PolyEx1 {
public static void main(String[] args) {
A refA = new A();
refA.method1(null);
}
}
class A {
public void method1(Object o) {
System.out.println("o");
}
public void method1(String str) {
System.out.println("str");
}
}
The output is "str", can someone explain me why str was printed? I am not able to understand this.
As explained by Rohit here,
That is because String class extends from Object and hence is more
specific to Object. So, compiler decides to invoke that method.
Remember, Compiler always chooses the most specific method to invoke.
If more than one member method is both accessible and applicable to a
method invocation, it is necessary to choose one to provide the
descriptor for the run-time method dispatch. The Java programming
language uses the rule that the most specific method is chosen.
The informal intuition is that one method is more specific than
another if any invocation handled by the first method could be passed
on to the other one without a compile-time type error.
However, if you have two methods with parameter - String, and Integer,
then you would get ambiguity error for null, as compiler cannot decide
which one is more specific, as they are non-covariant types.
As described in Section 15.12.5 of JLS
Java try to use the most specific applicable version of a method.
String extends Object and so the method using String as parameter is always called.
If you try to add a method that take an Integer input it will throws error as ambiguous methods, because String and Integer both of them are more specific than Object but none is more specific than the other one
public void method1(String str) will get executed because object is string supperclass so the string method will be called.
This question already has answers here:
Method overloading and choosing the most specific type
(9 answers)
Why this behavior in overloading [duplicate]
(3 answers)
Closed 9 years ago.
Just out of curiosity I tried this example.
public class Class1 {
public void method(Object obj){
System.out.println("Object");
}
public void method(String str){
System.out.println("String");
}
public static void main(String... arg){
new Class1().method(null);
}
}
The output being "String". I want to know on what basis the JVM decides to invoke method taking String as argument and not Object.
Whenever more than one overloaded methods can be applied to the argument list, the most specific method is used.
In this case either of the methods can be called when passing null, since the "null type" is assignable to both Object and to String. The method that takes String is more specific so it will be picked.
Whenever there's Method Overloading, the JVM will search for the method from the most specific type to least specific type
See the JLS specification
15.12.2.5. Choosing the Most Specific Method
It is one of the puzzle of Java Puzzlers by Joshua Bloch - Puzzle 46: Case of the Confusing Constructor
Java Compiler chooses the most specific method.
String is a more specific type compared to the Object.
Becaue you are passing null in calling method and you defined void method(String str)
And String always initlize with null. it will find that matching parametrized method.
Thats y u got "str" on console.
When you are doing method overloading, jvm tries to the next in hierarchy. For e.g. if you overload methods with long and int, but invoke method by passing byte, it will first go to int as it is next in hierarchy to byte.
It's because of method Overloading
The most specific method is chosen at compile time.
As 'java.lang.String' is a more specific type than 'java.lang.Object'. In your case it returns String.
This question already has answers here:
Which overload will get selected for null in Java?
(3 answers)
Closed 9 years ago.
I have the following code snippet:
public static void foo(Object x) {
System.out.println("Obj");
}
public static void foo(String x) {
System.out.println("Str");
}
If I call foo(null) why is there no ambiguity? Why does the program call foo(String x) instead of foo(Object x)?
why the program calls foo(String x) instead of foo(Object x)
That is because String class extends from Object and hence is more specific to Object. So, compiler decides to invoke that method. Remember, Compiler always chooses the most specific method to invoke. See Section 15.12.5 of JLS
If more than one member method is both accessible and applicable to a
method invocation, it is necessary to choose one to provide the
descriptor for the run-time method dispatch. The Java programming
language uses the rule that the most specific method is chosen.
The informal intuition is that one method is more specific than
another if any invocation handled by the first method could be passed
on to the other one without a compile-time type error.
However, if you have two methods with parameter - String, and Integer, then you would get ambiguity error for null, as compiler cannot decide which one is more specific, as they are non-covariant types.
The type of null is by definition a subtype of every other reference type. Quote JLS 4.1:
The null reference can always undergo a widening reference conversion to any reference type.
The resolution of the method signature involved in an invocation follows the principle of the most specific signature in the set of all compatible signatures. (JLS 15.12.2.5. Choosing the Most Specific Method).
Taken together this means that the String overload is chosen in your example.
It's calling the most specific method.
Since String is a subclass of Object, String is "more specific" than Object.
When given a choice between two methods where the argument is valid for both parameters, the compiler will always choose the most specific parameter as a match. In this case, null is a literal that can be handled as an Object and a String. String is more specific and a subclass of Object so the compiler uses it.
public class Main {
public void testMethod(Object o){
System.out.println("Object Method called");
}
public void testMethod(String s){
System.out.println("String Method called");
}
public static void main(String[] args) {
new Main().testMethod(null);
}
}
This program magically calls String method? On what criteria Java compiler decided to go with String method? Can somebody please point me the reason for this?
The rule is that the compiler chooses the "most specific" overload out of all matches. Since String is a subclass of Object, that makes the String version "more specific", and hence it is chosen
The operative part of the Java Language Specification is JLS 15.12.2.5:
"If more than one member method is both accessible and applicable to a method invocation, it is necessary to choose one to provide the descriptor for the run-time method dispatch. The Java programming language uses the rule that the most specific method is chosen.
The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error."
The JLS then proceeds to give a detailed technical specification of how to determine the most specific method.
Addressed here:
Overloaded method selection based on the parameter's real type