java Basics - Methods (review)

Keywords: Java

;

Java Procedure

1. What is method

  • definition

Methods are collections of statements that together perform a function
Method is an orderly 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

  • Principles of design method

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

2. Method definition and call

Definition of method

/*Method contains a method header and a method body.
	Modifier: optional. It defines the access type of the method and tells the compiler how to call the method.
	Return value type: the method may return a value. returnValueType is the data type of the return value of the method. If some methods have no return value, the returnValueType is the keyword void.
	Method name: is the actual name of the method. The method name and parameter table together constitute the method signature.
	Parameter type: like a placeholder. When a method is called, it passes a value to the parameter, which is called an argument or variable. Parameter list refers to the parameter type, order and number of parameters of a method. Parameters are optional, and methods can contain no parameters.
		Formal parameter: used to receive external input data when the 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

  • 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 add = Demo.add(1,2);
    
    • If the return value of the method is void, the method call must be a statement.
     Demo.hello();
    
    • Extension: Value Passing and reference passing (Java is value passing).
    • Call the methods of other classes. Unless it is a static method, you must instantiate this class (new)
    public class Demo {
        public static void main(String[] args) {
            int add = Demo.add(1,2); // Call static methods directly by class name
            Demo.hello(); 
            System.out.println(add); // 3
        }
    	// Static is a static method, otherwise it needs to be called by new instantiation
        public static int add(int a,int b){
            return a+b;
        }
    	// Static is a static method, otherwise it needs to be called by new instantiation
        public static void hello(){
            System.out.println("hello")
        }
    }
    

Value passing & reference passing

pass by value

//pass by value
public class Demo {
	public static void main(String[] args) {
		inta=1;
		System.out.println(a); //1
		Demo.change(a);
		System.out.println(a); //1
	}
	//The return value is null
	public static void change(int a){
		a=10;
	}
}

Reference passing

//Reference passing: object, essence or value passing
pub1ic class Demo {
	pub1ic static void main(String[] args) {
		Perosn perosn = new Perosn( );
		System.out.println(perosn. name); //null
		Demo.change(perosn);
		System.out.println(perosn.name); //Front sound
	}
	public static void change (Perosn perosn){
		//Perosn is an object: directional -- > perosn perosn = new perosn(); i
		perosn.name = "Front sound" ;
	}
	//Defines a Perosn class with -- attributes: name
	class Perosn{
		String name; //null
	}
}

3. Method overloading

Overloading is a function with the same function name but different formal parameters in a class

  • Rules for method overloading

    • Method names must be the same
    • The parameter list must be different (different number, different type, different parameter arrangement order, etc.)
    • The return types of methods can be the same or different.
    • Just different return types are not enough to overload methods.
  • Realization 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.

4. Command line parameters

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

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]);
    }
}

  1. As shown in the figure, enter javac Demo04.java in the command window
  2. When compiling a class file, an error will be reported if it is compiled in the current directory
  3. You need to return the directory to the package directory where the file is located, and then compile it
  4. The main function can pass some parameters. The parameter passed here is this is kuangshen
  5. It can be seen that the result is delivered successfully

5. Variable parameters

Starting with Jdk1.5, Java supports passing variable parameters of the same type to a method.
In the method declaration, add an ellipsis (...) after specifying the parameter type.
Only one variable parameter can be specified in a method. It must be the last parameter of the method.

//Print maximum
public static void printMax(int... num){
    if(num.length==0){
        System.out.println("No value passed in");
        return;
    }
    int result = num[0];
    for (int i = 1; i < num.length; i++) {
        if(num[i] > result){
            result = num[i];
        }
    }
    System.out.println("The maximum value is:"+result);
}
public static void main(String[] args) {
    printMax(1,2,3,4); //The maximum value is: 4
    printMax(new int[]{1,2,3,4,5}); //The maximum value is: 5
}

6. Recursion

  • Recursion is: method A calls method A and calls itself!

  • The recursive strategy only needs a small amount of code, which can describe the repeated calculation in the problem-solving process, which greatly reduces the amount of code of the program. The ability of recursion is to define an infinite set of objects with limited statements.

  • Recursive structure

    • Recursive header: when not calling its own method, no header will fall into an endless loop.
    • Recursive body: when to call its own method.
//Factorial n! n*(n-1)*...*2*1
public static int f(int n){
    if(n==1) return 1;
    return n*f(n-1); //Recursion: calling itself
}
public static void main(String[] args) {
    System.out.println(f(5)); //5!= 120
}

Video learning address: https://www.bilibili.com/video/BV12J41137hu?p=45

Posted by fl0w on Sat, 04 Sep 2021 13:19:31 -0700