The relationship of overload and method return type in Java?

前端 未结 4 1167
-上瘾入骨i
-上瘾入骨i 2020-12-06 21:04

If there are two methods, they have different parameters, and their return types are different. Like this:

int test(int p) {
   System.out.p         


        
4条回答
  •  一个人的身影
    2020-12-06 21:53

    To quote the official tutorial:

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

    Having a different return type is inconsequential to overloading. In fact, this is quite common with methods that return one of their arguments. E.g., java.util.Math has a bunch of overloaded max methods. A max of two ints return an int, a max of two doubles return a double, etc.

提交回复
热议问题