Class notes - Java method

What is method

System.out.println();
//Class (System) Object (out) Method (println())

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

1.Method is an ordered combination of steps to solve a class of problems
2.Method is contained in a class or object
3.Methods are created in the program and referenced elsewhere

Principle of design method: the original intention of the method is the function block, which is the collection of statement blocks to realize a certain function. When designing the method, it is best to keep the atomicity of the method, that is, a method only completes one function, which is conducive to later expansion.

Method definition and call

Definition of method

Java methods are similar to functions in other languages. They are code fragments used to complete specific functions. Generally, defining a method includes the following syntax:

Method contains a method header and a method body. Here are all parts of a method:

1.Modifier :Modifier, which is optional, tells the compiler how to call the method. Defines the access type of the method.
2.return type∶Method may return a value. returnValueType Is the data type of the return value of the method. Some methods perform the required operation but do not return a value. under these circumstances, returnValueType Is a keyword void. 
3.Method name:Is the actual name of the method. The method name and parameter table together constitute the method signature.
4.Parameter type:The parameter is like a placeholder. When a method is called, a value is passed to the parameter. This value 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 parameters:It is 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. For example:

int larger = max(30,40);

If the return value of the method is void, the method call must be a statement.

system.out.print1n( "Hello,zhangsan! ");

Expand your understanding after class: Value Passing and reference passing

Method overload

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

Rules for overloaded methods:

Method names must be the same.
The parameter list must be different(Different numbers, or types, 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.

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.

Command line parameters

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

public class CommandLine{
    public static void main(String args[]){
        for(int i = 0;i<args.length;i++){
            System.out.println("args["+i+"]:"+args[i])
        }
    }
}

[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-YzYbkDYy-1629195285945)(F:\Desktop\MarkDown learning \ classroom notes \ photo \ command line reference. png)]

Variable parameters

JDK1. Starting from 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. Any ordinary parameter must be declared before it.

public static void printMax(double...numbers){
    if(numbers.length == 0){
        System.out.println("No argument passed");
        return;
    }
    
    double result = numbers[0];
    
    //sort
    for(int i = 1;i < numbers.length; i++){
        if(numbers[i] > result){
            result = numbers[i];
        }
    }
    System.out.println("The max value is " + result);
}

recursion

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

Using recursion, we can solve some complex problems with simple programs. It usually transforms a large and complex problem layer by layer into a smaller problem similar to the original problem. The recursive strategy can describe the multiple repeated calculations required in the problem-solving process with only a small amount of program, 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.

The recursive structure consists of two parts:

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

​	2.Recursive body:When you need to call your own method.

The amount of code of the program is greatly reduced. The ability of recursion is to define an infinite set of objects with limited statements.

The recursive structure consists of two parts:

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

​	2.Recursive body:When you need to call your own method.

Keywords: Java

Added by cdog5000 on Mon, 20 Dec 2021 23:21:51 +0200