Java Serial 30-Method Overload, Method Recursion

1. Method overload

1. Also known as overload

2. Method overload usage scenarios

When functionality is similar, try to keep method names the same as possible (but when functionality is different or different, method names are as different as possible)

3. After what conditions are met, method overload can be constituted

(1) in the same class; (2) method names are different; (3) parameter lists are different: i. number is different; i I. order is different; i I i I. type is different

4. What does method overload have to do with and nothing to do with?

(1) Method overload is not related to method name + parameter list

(2) Method overload is independent of return value type

 

package code_class_file;

public class D30_overload {

  public static void main(String[] args) {

      m1();

      m4(2,4);

      m5();

  }

  //The following two methods make up an overload(Number is different)

  public static void m1() {}

  public static void m1(int a) {}

 

  //The following two ways make up an overload (in different order)

  public static void m2(int a,double b) {}

  public static void m2(double a,int b) {}

 

  //There are two ways to make a method overload (different types)

  public static void m3(int x) {}

  public static void m3(double x) {}

 

  //The following method compilation error is not a method overload, is a method duplication

  //public static void m4(int a,int b){}

  //public static void m4(int b,int a){}

 

  //The following method compilation error, which is not a method overload, is a method error

  //void m5(){}

  //public static void m5(){}

​

}

​

 

5. Specific application of method overload

Abbreviation printing operation

 

  public static void main(String[] args) {

    //Abbreviation printing operation

    p("jfshafo");

  }

  public static void p(byte a) {

    System.out.print(a);

  }

  public static void p(short a) {

    System.out.print(a);

  }

  public static void p(char a) {

    System.out.print(a);

  }

  public static void p(int a) {

    System.out.print(a);

  }

  public static void p(long a) {

    System.out.print(a);

  }

  public static void p(float a) {

    System.out.print(a);

  }

  public static void p(double a) {

    System.out.print(a);

  }

  public static void p(boolean a) {

    System.out.print(a);

  }

  public static void p(String a) {

    System.out.print(a);

  }

  public static void p(String[] a) {

    System.out.print(a);

  }

2. Method Recursion

1. Method Recursive Definition

Answer: A method call calls itself, called recursion

Example:

 

a(){

  a();

}

 

 

2. Recursion consumes stack memory and can be used without

The following program has an error (no exception, no error) that cannot be recovered when it occurs. The only result is that the JVM stops working.

 

    public static void main(String[] args) {

    dosome();

  }

  public static dosome() {

    p("dosome start");

    dosome();

    p("dosome End");       

  }

 

 

3. Recursion must have an end condition that will cause stack memory overflow

4. In some cases this functionality must be implemented recursively, for example, directory copy

5. Example: Recursively write an integer sum that calculates 1~N

 

  public static void main(String[] args) {

  int N=9;

  int resultSum = sum(N);

  System.out.println(resultSum);

  }

  public static int sum(int a) {

    if (a > 1) {

      return a+sum(a-1);

    }else {

    return 1;

    }

  }

3. Source code:

D30_overload_and_recursion.java

Address:

https://github.com/ruigege66/Java/blob/master/D30_overload_and_recursion.java

2.CSDN: https://blog.csdn.net/weixin_44630050 (Xinyue Jun Don't Know-Rui)

3. Blog Park: https://www.cnblogs.com/ruigege0000/

4. Welcome to the WeChat Public Number: Fourier Transform, Personal Public Number, only for learning and communication, Background Reply "Gift Pack" to get big data learning materials

 

Keywords: Java jvm github Big Data

Added by noguru on Sun, 15 Sep 2019 19:41:28 +0300