Java Methodology Explanation - Basic Stage

***

  1. What is the method

    Design Principles: The intent of a method is a function block, which is a collection of statement blocks that implement a function block. It is best to maintain atomicity (uniqueness)

    Java methods are a collection of statements that together perform a function:

    • Method is an ordered combination of steps to solve a class of problems

    • Method contained in class or object

    • Methods are created in programs and referenced elsewhere

      //Method Concepts
      public class Demo01 {
          //main method
          public static void main(String[] args) {
              int sum = add(9, 8);
              System.out.println(sum);
      
          }
      
          public static int add(int a,int b){
              return a+b;
          }
      }
      
  2. Definition and invocation of methods

    The method contains a method header and a method body. Specifically as follows:

    • Modifier: is optional and tells the compiler how to invoke the method, defining the type of access to the method.

    • Return value type: Method may have a return value. The returnValueType is the data type of the method return value. No return value is void.

    • Method name: The actual name of the method, the method name, and the parameter table together make up the method signature.

    • Parameter type: A parameter is like a placeholder, and when a method is called, a value is passed to the parameter, which is called an argument or variable. A list of parameters refers to the type, order, and number of parameters of a method. The parameters are optional and the method may not include any parameters.

      • Formal parameter: Data used to receive external input when a method is called.

      • Argument: The data actually passed to the method when the method is called.

    • Method body: The method body contains specific statements that define the function of the method.

      Modifier Return Value Type Method Name(Parameter Type Parameter Name){
          ...
          Method Body
          ...
          return Return value;
      }
      

    Method call (format: object name. method name (argument list):

    • When a method returns a value, the method call is usually treated as a value.
    • If the method return value is void, the method call must be a statement.
    public class Demo02 {
    
    
        public static void main(String[] args) {
    
            int max = max(10, 10);
            System.out.println(max);
    
        }
    
        //Compare Size
        public static int max(int num1,int num2){
               int result=0;
               if (num1==num2){
                   System.out.println("Guo Niubian");
                   return 0;//Termination Method
               }
               if (num1>num2){
                   result=num1;
               }else {
                   result=num2;
               }
               return result;
        }
    }
    
    
  3. Overload

    • Definition: An overload is a function in a class that has the same function name but different parameters.
    • Rule: Method names must be the same; The parameter list must be different; The return types of methods can be the same or different. Simply returning different types is not enough to be an overload of a method.
    • Implementation theory: When method names are the same, the compiler matches each method individually based on the number and type of parameters calling the method, to select the corresponding method. If the matching fails, the compiler will make an error.
    //Overload
    public class Demo03 {
    
        public static void main(String[] args) {
            // int max = max(10, 10);
            double max=max(10.0,10.0);
            System.out.println(max);
        }
    
    
        //Compare Size
        public static int max(int num1,int num2){
            int result=0;
    
            if (num1==num2){
                System.out.println("GUI is GUI");
                return 0;//Termination Method
            }
            if (num1>num2){
                result=num1;
            }else {
                result=num2;
            }
            return result;
        }
    
    
        //Compare Size
        public static double max(double num1,double num2){
            double result=0;
            if (num1==num2){
                System.out.println("GUI is not GUI");
                return 0;//Termination Method
            }
            if (num1>num2){
                result=num1;
            }else {
                result=num2;
            }
            return result;
        }
    }
    
    
  4. Command Line Passage (Understanding)

    //Command line arguments
    public class Demo04 {
        public static void main(String[] args) {
    
            for (int i = 0; i <args.length ; i++) {
                System.out.println("args["+i+"]"+args[i]);
            }
        }
    }
    
    
  5. Variable parameter (indefinite parameter): A method can only formulate one variable parameter, it must be the last parameter of the method, any common parameter must be declared before it.

    //Variable parameters
    public class Demo05 {
    
        public static void main(String[] args) {
    
            //Calling methods with variable parameters
            printMax(1,2,3,4,51);
            printMax(new double[]{1,2,5,10});
        }
    
        public static void printMax(double...numbers ){
            if (numbers.length==0){
                System.out.println("Ghost of Guo");
                return;
            }
    
            double result=numbers[0];
            
            //sort
            for (int j = 1; j <numbers.length ; j++) {
                if (numbers[j]>result){
                    result=numbers[j];
                }
            }
            System.out.println("Guo Gui is:"+result);
        }
    
    }
    
    
  6. recursion

Recursive: Method A calls method A and calls itself. Recursion includes a recursive header (deciding when to not call its own method and if there is no header, it falls into an infinite loop); Recursive body (determines when you need to call your own method).

public class Demo06 {
    public static void main(String[] args) {
        //Find factorial
        System.out.println(f(3));

    }

    public static int f(int i){
        if (i==1){
            return 1;
        }else {
            return i*f(i-1);
        }
    }
}

Keywords: Java

Added by redbullmarky on Wed, 02 Feb 2022 03:18:22 +0200