Java 中方法的签名是否包括它的返回类型?

Java 类/接口中的方法签名是否包括它的返回类型?

例如:

Java 知道这两种方法的区别吗:

public class Foo {
public int  myMethod(int param) {}
public char myMethod(int param) {}
}

还是只有方法名和参数列表才重要?

83835 次浏览

The method signature is the name and parameter list only.

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.

Java Language Spec says

Two methods have the same signature if they have the same name and argument types.

thus No, return type is not part of method signature.

no, in Java the method signature doesn't includes the return type, but the declaration does.

public             String         getString(String myString)


^access modifier   ^return type   ^name    ^parameter type and name

edited based on feedback below :)

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.

enter image description here

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.

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.

Using AspectJ (org.aspectj.lang.reflect.MethodSignature), it does have the return type

No not in Java. Method name and parameter list is only for method signature. Return type doesn't include.

Return type doesn't include in method signature.Only method name and Parameters are defined as method signature.

Reffer : Oracle Docs 'Defining Methods'

Bro, In java, we use to call methods by their name and their parameters only to use them in our code, like

myMethod(20, 40)

so, JAVA only searches for similar stuff matching in their corresponding declaration (name + param), this is why method signature only includes method's name and parameters. :)

In JAVA and many other languages, you can call a method without a variable to hold the return value. If return type is part of a method signature, there is no way to know which method will be called when calling without specifying variable holding return value.

The method signature is only the method's name and parameters. But I believe your example will generate an error if they were to be in the same class. You can simply test it out on any ide and see that the compiler will throw an error

THE METHOD SIGNATURE INCLUDES THE RETURN TYPE.

The compiler ignores it when has to check for duplicates. For Java is illegal to have two methods with the signature differing only by the return type.

Try that:

public class Called {
public String aMethod() {
return "";
}
}


public class Caller {
public static void main(String[] main) {
aMethod();
}
public static void aMethod() {
Called x = new Called();
x.aMethod();
}
}

Build the project, go to bin directory, copy the Caller.cass somewhere. Then change the called method:

public int aMethod() {
return 0;
}

Build the project, you will see that both Called.class and Caller.class have a new timestamp. Replace the Caller.class above and run the project. You'll have an exception:

java.lang.NoSuchMethodError: it.prova.Called.aMethod()Ljava/lang/String;

If you try to run the code you have mentioned on eclipse, you will have an answer as to what elements does java compiler looks for differentiating among java methods :

class Foo {
public int  myMethod(int param) {
return param;}
public char *myMethod*(int param) { //this line throws an error
return param;
}
}

The error thrown is : Duplicate method myMethod(int) in type Foo .