Use of Java methods

    Simply put, methods in Java are similar to functions in C. For example, the main method main method, in C language, is the main function. All programs start with the main method and end with the main method. Below, a brief review of the methods in Java.

The concept and use of the method

1. What is the method

A method is a block (fragment) of code, similar to a function in the C language. We know that in the C language, the existence of functions makes the code modular and greatly improves the efficiency of the code. In Java, methods do the same thing:

(1) Make the code reusable.

(2) Make the code modular.

(3) Directly call existing methods, such as Java-encapsulated methods, to avoid repeating the creation of wheels.

2. Definition of method

Similar to the C language function, the format of the method is as follows:

// method definition

modifier return value type method name([parameter type parameter...]) {

      method body code;

      [return return value];

}

    The thing to explain here is the modifier. Since Java is an object-oriented language, when writing code, the implementer of the method hopes that the caller of the method cannot modify the method he writes, so he must encapsulate it by qualifying the modifier private, which is a modifier. Similar to public and so on.

Now, let's write a method to add two numbers:

public static int add(int x, int y) {
    return x + y;
}

public static void main(String[] args) {
    int a = 10;
    int b = 10;
    System.out.println(add(a, b));
}

    The modifiers here are public and static, which are public permissions, indicating that this method can be called by any method. And can be modified by the caller of the method. Of course not. Static indicates that this method is a static method, because the main method is static, and the method to be called can only be static:

The specific reasons to go to classes and objects to explain. It is recommended to add public and static here.

Precautions:

(1) The return value type of the method: if there is, the return value type should be the same as the returned entity type, otherwise it should be written as void.

(2) The name of the method: it should be named with a small camel case.

(3) The parameter list of the method: if not, write nothing, otherwise, you need to specify the parameter type, and use "," to separate multiple parameters.

(4) Method body: The code block that implements the function.

(5) The method should be written in the class. The add method above is written in a public main class.

(6) There is no declaration for the method, nor can it be nested.

3. Method call

    The method is called in the main function, and the parameters are passed in. Then, the memory will look for the address of the method. After finding it, execute the method body of the called method. After the called method ends, return the result and return to the calling method to continue execution. . A method that can be called multiple times. When a method is defined, the code of the method is not executed, only when it is called.

Having said that, it is necessary to explain the actual parameters and formal parameters.

A formal parameter is a temporary copy of an actual parameter. A formal parameter and an actual parameter are two different entities, and changing the formal parameter has no effect on the actual parameter. for example:

public static void swap(int x, int y) {
    int tmp = x;
    x = y;
    y = tmp;
    System.out.println(x + " " +  y);
}
    
public static void main(String[] args) {
    int a = 10;
    int b = 20;
    swap(a, b);
    System.out.println(a + " " +  b);
    }

The result is obvious.

    The actual parameters a and b are two variables in the main method, and their space is in the stack of the main method (a special memory space, Java cannot get this memory, the C language only needs &), and The formal parameters x and y are two variables in the swap method, and the space of x and y is in the stack when the swap method runs. Therefore: the actual parameters a and b and the formal parameters x and y are two variables that have no relationship , when the swap method is called, only a copy of the values ​​in the actual parameters a and b is passed to the formal parameters x and y, so the operation on the formal parameters x and y will not have any effect on the actual parameters a and b.

    In the C language, if you want to change the value of the actual parameter, you need to use a pointer, but Java has no concept of a pointer. To exchange, you can use arrays, classes and objects to achieve. That is to say, in Java, all calls are call-by-value, there is no call-by-reference, and the address is also a value.

To do this, use an array to do it:

2. Method overloading

1. Why do you need method overloading?

    For example, how to add two numbers. Two ints are written in the parameter list. When we pass them, we can only pass int parameters. Now if you want to realize the addition of doubles, then you are writing a new method and providing a new method name, but it is a headache to choose a name. And method overloading allows us to put new wine in old bottles.

 2. The concept of method overloading

    In Java, if multiple methods have the same name and different parameter lists, the methods are said to be overloaded. For example, several official methods are overloaded, take Arrays.copyOf (array copy) as an example:

 Here, the copyOf method is overloaded so that it can copy integers, characters, etc.

3. The main points of method overloading

(1) The method name must be the same

(2) The parameter list should be different, such as the parameter order of the parameter list, the number of parameters, and the parameter type. Parameter names do not matter.

(3) The return value is not required 

    When the compiler compiles the code, it will deduce the type of the actual parameter, and determine which method to call according to the result of the deduction. Then the names of the above methods are the same, why not report an error after overloading, which involves the signature of the method, just understand.

3. The signature of the method

    The method signature is the final name of the method after being compiled and modified by the compiler. The specific method: the full path name of the method + the parameter list + the return value type, which constitutes the complete name of the method.

To understand the signature of a method, Java assembly tools are used. After assembly, the following information is obtained:

add:(DD)D, the two D in brackets indicate double, the outer D indicates that the return type is double, and the preceding test is the path of the method.

Third, the recursion of the method

The recursion of the method in Java is the same as the recursion of the function in the C language, both of which turn a big problem into a small problem. Here is an example of mine.

icon-default.png?t=M276Use recursion to implement n^k C language Good, but difficult to understand and a lot of wasted space. Some problems are naturally suitable for recursion, such as binary tree problems. Recursion takes a lot of practice.

 

 

 

Guess you like

Origin blog.csdn.net/Naion/article/details/123471570