Does a method's signature in Java include its return type?

谁说我不能喝 提交于 2019-12-27 10:36:29

问题


Does the method signature in a Java class/interface include its return type?

Example:

Does Java know the difference between those two methods:

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

Or is it maybe only the method name and parameters list that matter?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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




回答5:


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.




回答6:


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.




回答7:


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. :)




回答8:


The method signature is the name and parameter list only.




回答9:


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 :)




回答10:


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

Reffer : Oracle Docs 'Defining Methods'




回答11:


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




回答12:


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;



回答13:


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




回答14:


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 .



来源:https://stackoverflow.com/questions/16149285/does-a-methods-signature-in-java-include-its-return-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!