Java Learning Notes Method overloading

Source: Internet
Author: User

The overloaded method must have a different parameter list. Methods cannot be overloaded based on different modifiers or return value types.

 PackageWelcome; Public classtestmethodoverloading { Public Static voidMain (string[] args) {System.out.println ("The maximum between 3 and 4 is" + MAX (3, 4));//call the max (int, int) methodSystem.out.println ("The maximum between 3.0 and 5.4 is" + max (3.0, 5.4));//Call the Max (double, double) methodSystem.out.println ("The maximum between 3.0, 5.4 and 10.14 is" + max (3.0, 5.4, 10.14));//Call the Max (double, double, double) method    }         Public Static intMaxintNUM1,intnum2) {        if(Num1 >num2) {            returnNUM1; }Else{            returnnum2; }    }         Public Static DoubleMaxDoubleNUM1,Doublenum2) {        if(Num1 >num2) {            returnNUM1; }Else{            returnnum2; }    }         Public Static DoubleMaxDoubleNUM1,DoubleNUM2,Doublenum3) {        returnMax (Max (num1,num2), num3); }}

 PackageWelcome;/** Demo Ambiguous call * When a method is called with one or more possible matches, the compiler cannot determine which match is more precise, which is called ambiguous invocation. * Ambiguous calls throw compilation errors*/ Public classambiguousoverloading { Public Static voidMain (string[] args) {System.out.println (max ());//max (int, double) and Max (double, int) are all likely to match Max, because neither of these methods is more precise than who}//so this call is ambiguous and will throw a compilation error         Public Static DoubleMaxintNUM1,Doublenum2) {        if(Num1 >num2) {            returnNUM1; }Else{            returnnum2; }    }         Public Static DoubleMaxDoubleNUM1,intnum2) {        if(Num1 >num2) {            returnNUM1; }Else{            returnnum2; }    }}

Java Learning Notes Method overloading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.