Home > Java > JavaBase > body text

What is method overloading?

青灯夜游
Release: 2020-07-18 13:32:43
Original
5998 people have browsed it

Method overloading refers to defining multiple methods with the same name in a class, but each method is required to have different parameter types or number of parameters. Method overloading can occur either in ordinary methods or in constructor methods. Method overloading can occur in the same class or in parent and child classes.

What is method overloading?

#Method overloading means repeated method names and different loading parameters.

In a Java class, multiple methods with the same name are defined. If the method names are the same but the method parameters are different, including the type and number of parameters, it is called method overloading. When an overloaded method is called, the Java compiler selects an appropriate method by checking the parameter types and number of the called method. Method overloading is often used to create methods that accomplish a similar set of tasks but differ in the type of parameters or the number of parameters or the order of parameters.

Overloading of methods can occur in ordinary methods or constructors. Method overloading can occur in the same class or in parent and child classes.

Overloading of Java methods: return types and modifiers can be the same or different. Methods with the same name must have different parameter lists. Only different return types are not enough to distinguish two overloaded methods.

Specific specifications

1. The method names must be the same.

2. The parameter lists of methods must be different, including the type or number of parameters, to distinguish different method bodies.

1) If the number of parameters is different, the parameter type is ignored!

2) If the number of parameters is the same, the types of parameters must be different.

3. The return types and modifiers of methods can be the same or different.

Example:

Question requirement: Compare two data to see if they are equal.

The parameter types are two byte types, two short types, two int types, and two long types.

And test it in the main method

public class CaiNiao{
    
    public static void main(String[] args){
        byte a = 10;
        byte b = 20;
        System.out.println(isSame(a,b));
        
        System.out.println((isSame(short)20,(short)20));
        
        System.out.println(isSame(11,22));
        
        System.out.println(isSame(10L,10L));
    }
    
    public static boolean isSame(byte a,byte b){
        System.out.println("两byte参数的方法执行!");
        boolean same ;
        if(a==b){
            same = true;
        }else{
            same = false;
        }
        return same;
    }

    public static boolean isSame(short a,short b){
        System.out.println("两short参数的方法执行!");
        boolean same = a == b ?true:false;
        return same;
    }
    
    public static boolean isSame(int a,int b){
        System.out.println("两int参数的方法执行!");
        return a == b:;
    }
    
    public static boolean isSame(long a,long b){
        System.out.println("两long参数的方法执行!");
        if (a==b){
            return true;
        }
        else{
            return false;
        }
    }
}
Copy after login

Note that Java method overloading requires that methods with the same name must have different parameter lists. Only different return types are not enough to distinguish between two overloadings. loading method.

Recommended tutorial: Java tutorial

The above is the detailed content of What is method overloading?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template