Overloads of the Java-I method

Source: Internet
Author: User

Overloading of methods

We assume that we want to implement a method of summing numbers in a program, because the number and type of the sums involved are uncertain, so we need to design different methods for different situations. Next, a case is implemented to add two integers, add three integers, and add two decimals, as shown below. Methoddemo02.java

Public class MethodDemo02 {

Public Static void Main (string[] args) {

The following is a call to the summation method

int sum1 = add01(1, 2);

int sum2 = add02(1, 2, 3);

double sum3 = add03(1.2, 2.3);

The following code is the result of printing the sum

System. 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 int add01 (int x, int y) {

return x + y;

}

The following method implements the addition of three integers

Public Static int add02 (int x, int y, int z) {

return x + y + z;

}

The following method implements the addition of two decimals

Public Static double add03 (double x, double y) {

return x + y;

}

}

The result of the operation is as shown.

Figure 1-1 Running Results

It is not difficult to see from the above code that the program needs to define a method for each summation case, and if each method has a different name, it is difficult to distinguish which method to call when calling.

To solve this problem, 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.

The following three methods are overloaded with each other

l public static int add (int x,int y) {logical}//Two integer addition

l public static int Add (int x,int y,int z) {logical}//Three integer addition

l public static int add (double x,double y) {logic}//Two decimal additions

The next step is to modify it by means of a method overload, as shown below. Methoddemo03.java

Public class MethodDemo03 {

Public Static void Main (string[] args) {

The following is a call to the summation method

int sum1 = Add(1, 2);

int sum2 = Add(1, 2, 3);

double sum3 = Add(1.2, 2.3);

The following code is the result of printing the sum

System. 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 int Add (int x, int y) {

return x + y;

}

The following method implements the addition of three integers

Public Static int Add (int x, int y, int z) {

return x + y + z;

}

The following method implements the addition of two decimals

Public Static double Add (double x, double y) {

return x + y;

}

}

The Methoddemo02.java result is the same as the Methoddemo03.java, as shown in.

Figure 1-2 Running Results

The code above defines three add () methods with the same name, with different number or types of arguments, resulting in overloading of the method.

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.

Considerations for Overloading

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)

  Overloads are only related to the method name and the parameter type, not the return value

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

  Overloading is independent of the specific variable identifier

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

Overloads of the Java-I method

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.