Java Method overloading

Source: Internet
Author: User

Java allows you to define multiple methods with the same name in a class, but the type or number of parameters must be different, which is the overload of the method.

 Public classMethodDemo03 { Public Static voidMain (string[] args) {//The following is a call to the summation method        intSUM1 = Add (1, 2); intsum2 = Add (1, 2, 3); DoubleSUM3 = Add (1.2, 2.3); //The following code is the result of printing the sumSystem.out.println ("sum1=" +sum1); System.out.println ("Sum2=" +sum2); System.out.println ("Sum3=" +sum3); }    //The following method implements the addition of two integers     Public Static intAddintXinty) {returnX +y; }    //The following method implements the addition of three integers     Public Static intAddintXintYintz) {returnX + y +Z; }    //The following method implements the addition of two decimals     Public Static DoubleAddDoubleXDoubley) {returnX +y; }}

When the Add () method is called in the main () method, it is possible to determine which overloaded method is called by passing in different parameters, such as Add, which is a method of summing two integers. It is worth noting that the overloads of a method are independent of the return value type, which has only two conditions, one is the same method name, the other is the number of arguments or the parameter type is different.

L Overloaded method parameters must be different:

Different number of arguments, such as method (int x) and method (int x,int y)

parameter types are different, such as method (int x) and method (Double x) different from G

parameter order differs, such as method (int x,double y) differs from method (double x,int y)

The L overload is only related to the method name and the parameter type, regardless of the return value

such as void method (int x) and int method (int y) are not methods overloads and cannot exist concurrently

L overloads are independent of specific variable identifiers

such as method (int x) and approach (int y) are not methods overloaded and cannot exist simultaneously

Java Method overloading

Related Article

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.