This question already has answers here:
Which overload will get selected for null in Java?
(3 answers)
Closed 8 years ago.
When i run this code the output is "String"; if I hide the method that accepts a String parameter and run the code again then the output is "Object", so can anyone please explain me how this code works?
public class Example {
static void method(Object obj) {
System.out.println("Object");
}
static void method(String str) {
System.out.println("String");
}
public static void main(String args[]) {
method(null);
}
}
Compiler will choose most specific method, in this case, String is a sub class of Object, so the method with String as argument will be invoked.
From 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.
Related
This question already has answers here:
Passing null to the Overloaded methods [duplicate]
(3 answers)
How to do method overloading for null argument?
(7 answers)
Closed 4 years ago.
package java_coding;
public class String_Null {
public static void main(String[] args) {
// TODO Auto-generated method stub
String_Null string_Null = new String_Null();
string_Null.method(null);
}
public void method(String string) {
System.out.println("String");
}
public void method( Object object) {
System.out.println("Object");
}
}
The result is String. Explain why?
Because the most specific method is chosen, the most specific is String (compared to Object). This is in the JLS btw... I'll dig this up shortly.
According to the 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 error.
This question already has answers here:
Is it possible to have different return types for a overloaded method?
(13 answers)
The relationship of overload and method return type in Java?
(4 answers)
Closed 6 years ago.
I am gonna put this question to have a clear idea about overloading Concept in java . As per my understanding while method resolution in overloading compiler will look for method signature that is it should have same method name and different argument types . But what if the return type is different ??
class Test{
public void m1(int i) {
System.out.println(" int arg");
}
public int m1(String s) {
System.out.println("String-arg");
return (5+10);
}
public static void main (String[] args) throws java.lang.Exception
{
Test t = new Test();
t.m1(5);
int i = t.m1("ani");
System.out.println(i);
}}
the above program is running perfectly . my doubt here is , the method m1() is it overloaded ?? it has different return type . someone please make it clear. Thanks in advance
In Java methods are identified by name and arguments' classes and amount. The return type doesn't identify the method. For this reason the following code would be illegal:
public void m1(String i) {
System.out.println(" int arg");
}
public int m1(String s) {
System.out.println("String-arg");
return (5+10);
}
If two methods of a class (whether both declared in the same class, or both inherited by a class, or one declared and one inherited) have the same name but signatures that are not override-equivalent, then the method name is said to be overloaded. (...) When a method is invoked (§15.12), the number of actual arguments (and any explicit type arguments) and the compile-time types of the arguments are used, at compile time, to determine the signature of the method that will be invoked (§15.12.2). If the method that is to be invoked is an instance method, the actual method to be invoked will be determined at run time, using dynamic method lookup (§15.12.4)
Summarizing, two methods with the same name can return different types, however it's not being taken into account when deciding which method to call. JVM first decides which method to call and later checks if the return type of that method can be assigned to the certain variable.
Example (try to avoid such constructions):
public int pingPong(int i) {
return i;
}
public String pingPong(String s) {
return s;
}
public boolean pingPong(boolean b) {
return b;
}
if we follow the Oracle definition then yes, it is a overloaded method
here the info (emphasis mine)
The Java programming language supports overloading methods, and Java
can distinguish between methods with different method signatures. This
means that methods within a class can have the same name if they have
different parameter lists (there are some qualifications to this that
will be discussed in the lesson titled "Interfaces and Inheritance").
the fact that the method return a value or not is IRRELEVANT for the overloading definition...
another thing is here why can a method somethimes return a value and sometimes no...
this will drive crazy the people using the code, but that is another question...
This question already has answers here:
How to do method overloading for null argument?
(7 answers)
Closed 8 years ago.
I have the following 2 methods overloaded in a class :
public class Test{
public static void main(String []args) throws ParseException{
Test t = new Test();
t.testMethod(null);
}
public void testMethod(Object o){
System.out.println("Object");
}
public void testMethod(String s){
System.out.println("String");
}
}
When I invoke the method testMethod it print "String".
When I add one more overloaded method :
public void testMethod(StringBuilder sb){
System.out.println("String");
}
It throws me compiler error : The method testMethod is ambigous for type Test..
All this happens when I invoke the method with null
My questions are :
Why it prints String and not Object?
Why is there compilation error on adding third method?
Method resolution works by selecting the most specific method that is applicable for the given arguments. In your first case, String is more "specific" than Object (since it is a subclass of Object) and so the String method is chosen.
When you add another method that takes a StringBuilder, both that and the String method are equally "specific" (they are both direct subclasses of Object), so there is an ambiguity as to which should be chosen, hence the error.
Why it prints String and not Object?
The java compiler picks the method with the most specific or least generic argument. Since Object is the Superclass of all classes (including String), String class is picked.
Why is there compilation error on adding third method?
Since String and StringBuilder are below Object, the compiler will find the call ambiguous since both String and StringBuilder can accept null, the compiler fails to determine which method to call hence you get error during compilation.
If you try the same thing with IOException and FileNotFoundException instead of String and StringBuilder, you will find out that FileNotFoundException is picked since it is least generic.
In case1 String inherits from Object, they are in inheritance chain, null matches both, compiler chooses a more specific type - String.
In case 2 String and StringBuilder are not in inheritance chain, compiler cannot choose a more specific type.
Details of overloaded method resolution are described in JLS 15.12.2.
This question already has answers here:
How do overloaded methods work?
(2 answers)
Closed 8 years ago.
I have a dummy program in java given below-
public class DummyTest {
public static void main(String[] args) {
hungry(null);
}
public static void hungry(Object o){
System.out.println("object");
}
public static void hungry(String s){
System.out.println("string");
}
}
this program returns prints string. please tell me the concept that why it prints string and not object.
This is how method overloading works. When the parameter of one candidate is more specific than the parameter of the other (as String is more specific than Object), the method with the more specific parameter is chosen.
Note that if you add a 3rd hungry method with a parameter of reference type unrelated to String (for example Integer), the code won't pass compilation since the compiler won't have a preference between hungry(String) and hungry(Integer).
This question already has answers here:
Java method dispatch with null argument
(4 answers)
Closed 9 years ago.
public class Overloading {
public static void main(String[] args) {
Overloading o = new Overloading();
o.display(null);
}
void display(String s) {
System.out.println("String method called");
}
void display(Object obj) {
System.out.println("Object method called");
}
}
It is giving the output as "String method called". I need the explanation why?
Taken from the Java Spec:
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.
First of all: both methods are accessible (obviously) and applicable. They are both applicable, because null is of type nulltype, which is by definition a subtype of all types. String is more specific than Object, because String extends Object. If you would add the following, you will have a problem, because both Integer and String are equally "specific":
void display(Integer s) {
System.out.println("Integer method called");
}
Java will always find the most specific version of method that is available.
String extends Object, and therefore it is more specific.
http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.5