[Differences between Java and C ++] method Overloading

Source: Internet
Author: User

Similarities:

All methods in the same class have the same name, and different parameter Methods Form overload.

There will be a problem of type matching when passing parameters:

add(1,2) ;public void add(double a, float b) {...}public void add(float a, double b) {...}


Differences:

For C ++, overloading can also occur between the const and non-const versions with the same parameters. However, Java does not. For Java, functions of final and non-final versions with the same parameters do not constitute overloading. (The Compiler reports an error)

public void xx1(int a) { ... } public void xx1(final int c) { ... } /* these are not overload methods, the xx1 is repeaed defined */ 

For variable parameters

public void test (String msg) {...}public void test (String... msgs) {...}obj.test("Tom") ; /* there call the firest test *//* follow call the second test */obj.test("Evan, Tank) ;obj.test() ; 

It can be seen that this method is called only when the method of the non-Variable Parameter exactly matches, otherwise it will call the variable parameter.

Ambiguity arising from overload of Variable Parameter Methods

public void go(int ... nums) {...}              /* at least has 0 parameter */public void go(int a, int... nums) {...}          /* at least has 1 parameter */public void go(int a, int b, int... nums) {...}   /* at least has 2 parameters */ obj.go(1) ;     /* can be called by firest and second method */ obj.go(1,2);    /* can be called by 1st,2nd,3th methods */obj.go(1,2,3) ; /* can be called by 1st, 2nd, 3th methods */


In the preceding three go statements, the difference lies in the limit on the minimum number of parameters. If one calls the number of parameters it passes> = two or more overload functions, for this call, the overload method that satisfies his parameters is his best match, which may lead to ambiguity. For the above obj. Go (1) method, both the first and second methods are the best match, which leads to the ambiguity during compilation.

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.