两个 Java 方法是否可以使用相同的名称和不同的返回类型?

两个 Java 方法的 同一个名字是否具有不同的 报税表类别?这些方法的返回类型不同,它们使用相同的方法名声明。

这样可以吗?

149838 次浏览

Only if their parameter declarations are different from memory.

Only, if they accept different parameters. If there are no parameters, then you must have different names.

int doSomething(String s);
String doSomething(int); // this is fine




int doSomething(String s);
String doSomething(String s); // this is not

No. C++ and Java both disallow overloading on a functions's return type. The reason is that overloading on return-type can be confusing (it can be hard for developers to predict which overload will be called). In fact, there are those who argue that any overloading can be confusing in this respect and recommend against it, but even those who favor overloading seem to agree that this particular form is too confusing.

If both methods have same parameter types, but different return type than it is not possible. From Java Language Specification, Java SE 8 Edition, §8.4.2. Method Signature:

Two methods or constructors, M and N, have the same signature if they have the same name, the same type parameters (if any) (§8.4.4), and, after adapting the formal parameter types of N to the the type parameters of M, the same formal parameter types.

If both methods has different parameter types (so, they have different signature), then it is possible. It is called overloading.

You can have two methods with the same arguments and different return types only if one of the methods is inherited and the return types are compatible.

For example:

public class A
{
Object foo() { return null; }
}


public class B
extends A
{
String foo() { return null; }
}

According the JLS, you cannot however due to a feature/bug in the Java 6/7 compiler (Oracle's JDK, OpenJDK, IBM's JDK) you can have different return types for the same method signature if you use generics.

public class Main {
public static void main(String... args) {
Main.<Integer>print();
Main.<Short>print();
Main.<Byte>print();
Main.<Void>print();
}


public static <T extends Integer> int print() {
System.out.println("here - Integer");
return 0;
}
public static <T extends Short> short print() {
System.out.println("here - Short");
return 0;
}
public static <T extends Byte> byte print() {
System.out.println("here - Byte");
return 0;
}
public static <T extends Void> void print() {
System.out.println("here - Void");
}
}

Prints

here - Integer
here - Short
here - Byte
here - Void

For more details, read my article here

If it's in the same class with the equal number of parameters with the same types and order, then it is not possible for example:

int methoda(String a,int b) {
return b;
}
String methoda(String b,int c) {
return b;
}

if the number of parameters and their types is same but order is different then it is possible since it results in method overloading. It means if the method signature is same which includes method name with number of parameters and their types and the order they are defined.

The java documentation states:

The compiler does not consider return type when differentiating methods, so you cannot declare two methods with the same signature even if they have a different return type.

See: http://docs.oracle.com/javase/tutorial/java/javaOO/methods.html

Even if it is an old thread, maybe some is interested.

If it is an option to you to use the same method inside the same class and archive different return types, use generics: Oracle Lesson Generics

Simple example for generic value holder class:

class GenericValue<T> {
private T myValue;


public GenericValue(T myValue) { this.myValue = myValue; }


public T getVal() { return myValue; }
}

And use it like this:

public class ExampleGenericValue {
public static void main(String[] args) {
GenericValue<Integer> intVal = new GenericValue<Integer>(10);
GenericValue<String> strVal = new GenericValue<String>("go on ...");


System.out.format("I: %d\nS: %s\n", intVal.getVal(), strVal.getVal());
}
}

... will result in the following output:

I: 10
S: go on ...