Does a method's signature in Java include its return type?
Quoting from Oracle Docs:
Definition: Two of the components of a method declaration comprise the method signature—the method's name and the parameter types.
Since the question was edited to include this example:
public class Foo {
public int myMethod(int param) {}
public char myMethod(int param) {}
}
No, the compiler won't know the difference, as their signature: myMethod(int param)
is the same. The second line:
public char myMethod(int param) {}
will give you can error: method is already defined in class, which further confirms the above statement.
Is class method signature in Java includes return type ?
In Java, it doesn't but in this JVM it does which can lead to obvious confusion.
Is interface method signature in Java includes return type ?
The same as for class methods.
Or only method name and parameters list ?
Method name and parameter types for Java. For example, the parameter annotations and names don't matter.
At bytecode level, "return type" is part of method signature. Consider this
public class Test1 {
public Test1 clone() throws CloneNotSupportedException {
return (Test1) super.clone();
}
}
in bytecode there are 2 clone() methods
public clone()LTest1; throws java/lang/CloneNotSupportedException
public clone()Ljava/lang/Object; throws java/lang/CloneNotSupportedException
they differ only by return type.