JAVA classroom notes 3 method

Posted by keziah on Tue, 25 Jan 2022 00:30:02 +0100

1, What is method

  1. System.out.println(), so what is it?

    Call the method println() in the standard output object out in the System class System.

  2. A Java method is a collection of statements that together perform a function.

    • Method is an ordered combination of steps to solve a class of problems
    • Method is contained in a class or object
    • Methods are created in the program and referenced elsewhere
  3. Principles of design method:

    The original meaning of method is function block, which is the collection of statement blocks to realize a function. When designing methods, we'd better keep the atomicity of methods, that is, one method only completes one function, which is conducive to our later expansion.

2, Method definition and call

  1. Definition of method

    [Modifier ]  Return value type method name([parameter list]) {
    	Method body
        return Return value;
    }
    Note:
        Except for abstract methods, all other methods must have a method body.
    
    public class Demo01 {
        public static void main(String[] args) {
            int sum = add(1,6);//Arguments: parameters passed to the function / method at call time
            System.out.println(sum);
        }
        //addition
        public static int add(int a,int b){//Formal parameters: receive the parameters passed in when calling the function / method, like placeholders
            return a+b;
        }
    }
    
  2. Method call

    • Calling method: object name Method name (argument list)

    • Java supports two methods of calling methods, which are selected according to whether the method returns a value

      • When a method returns a value, the method call is usually treated as a value

        int larger = max(30,40);
        
      • When the return value of a method is void, the method call must be a statement

        System.out.println("hello");
        
      public class Demo02 {
          public static void main(String[] args) {
              int max = max(10,20);//int max = max(10,10);
              System.out.println(max);
          }
          public static int max(int num1,int num2){
              int result = 0;
              if (num1==num2){
                  System.out.println("num1=num2");
                  return 0;//Termination method
              }
              if (num1>num2){
                  result = num1;
                  System.out.println("num1>num2");
              }else {
                  result = num2;
                  System.out.println("num1<num2");
              }
              return result;
          }
          /*
          Result 1:
              num1<num2
              20    
          Result 2:
              num1=num2
              0    
          */
      }
      

3, Method overload

  1. Method overloading means that multiple methods of a class have the same name, but the parameter lists of these methods are different: the number of parameters or the type of parameters are different.

  2. Rules for method overloading:

    • Method names must be the same
    • The parameter list must be different (different number, different type, different order of parameters, etc.)
    • The return value type of the method and the name of the parameter are not involved in the comparison and have nothing to do with overloading
    • Method overloading is a kind of polymorphism
  3. Implementation theory:

    If the method names are the same, the compiler will match them one by one according to the number of parameters and parameter types of the calling method to select the corresponding method. If the matching fails, the compiler will report an error.

    public class Demo02 {
        public static void main(String[] args) {
            //max(10,40);
            double max = max(10,90);//Parameter type int
            System.out.println(max);
        }
        public static double max(int num1,int num2){//Parameter type int
            double result = 0;
            if (num1==num2){
                System.out.println("num1=num2");
                return 0;//Termination method
            }
            if (num1>num2){
                result = (double) num1;
                System.out.println("num1>num2");
            }else {
                result = (double) num2;
                System.out.println("num1<num2");
            }
            return result;
        }
        public static int max(double num1,double num2){//Parameter type double
            int result = 0;
            if (num1==num2){
                System.out.println("num1=num2");
                return 0;//Termination method
            }
            if (num1>num2){
                result = (int) num1;
                System.out.println("num1>num2");
            }else {
                result = (int) num2;
                System.out.println("num1<num2");
            }
            return result;
        }
        /*
        result:
            num1<num2
            90.0    
        */
    }
    

4, Command line parameters

  1. Sometimes you want to pass messages to a program when it runs. This is achieved by passing command-line parameters to the main() function.

    public class Demo03 {
        public static void main(String[] args) {
            //args.length array length
            for (int i=0;i<args.length;i++){
                System.out.println("args["+i+"]:"+args[i]);
            }
        }
    }
    
  2. Enter in Terminal

    E:\IDEA\JAVA\JavaSE\Basic grammar>cd src
    
    E:\IDEA\JAVA\JavaSE\Basic grammar\src>java com.zanbimo.method.Demo03 this is
    args[0]:this
    args[1]:is
    

5, Variable parameters

  1. JDK1. Starting from 5, Java supports passing variable parameters of the same type to a method.

  2. In the method declaration, add an ellipsis (...) after specifying the parameter type.

  3. Only one variable parameter can be specified in a method. It must be the last parameter of the method. Any ordinary parameter must be declared before it.

    public class Demo04 {
        public static void main(String[] args) {
            Demo04 demo04 = new Demo04();
            demo04.test("ab",1,2,3);
            System.out.println();
            demo04.test("AB",new int[]{11,22,33});
        }
        public void test(String a,int... number){
            System.out.print(a+"\t");
            for (int i=0;i<number.length;i++){
                System.out.print(number[i]+"\t");
            }
        }
        /*
        result:
            ab	1	2	3	
            AB	11	22	33
        */
    }
    

6, Recursion

  1. Recursion is: method A calls method A! Is to call yourself

    Usually, a large and complex problem is transformed into a smaller problem similar to the original problem

  2. The recursive structure consists of two parts:

    • Recursive header: when not to call its own method. If there is no head, it will fall into a dead cycle.

    • Recursive body: when to call its own method.

      public class Demo05 {
          public static void main(String[] args) {
              System.out.println(f(3));//n factorial
          }
          public static int f(int n){
              if (n==1){
                  return 1;//Recursive header
              }else {
                  return n*f(n-1);//Recursive body
              }
          }
          /*
          result:
          	6
          */
      }
      

Topics: Java