07 method of JAVA learning progress

Keywords: Java

Basic syntax of method

A method is a piece of code that can complete a specific function and can be used many times. A method becomes a function in C

Methods are defined in the class body. A class body can define multiple methods. The order of methods can be arbitrary, but methods cannot be defined in methods

Definition of method

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

        // Method body

}

The modifier list is optional but not required. It can now be written as public static [to be covered later]

return type

The returned value is the specific existing data, and the data has types; void returns NULL; if it is not void, a specific data must be returned

The initial letter of the method name is lowercase, preferably a verb. See the name to know the meaning.

Formal parameters are local variables. The number is 0~N, separated by commas. The number of formal parameters and arguments must be the same and the data type must be the same.

How can static call this method? Class name. Method name (actual parameter list);

public class MethodTest {

    //Class body
    //JAVA statements cannot be written directly in the class body, except for declaring variables
    //Method appears in the class body


    //method
    //Public is public, static is static, and void is the end of method execution without returning any data
    //Main is the method name: main method
    //(String[] args) parameter list. String [] is the data type of the parameter and args is the variable name of the local variable
    //Therefore, only args can change the variable name at will
    public static void main(String[] args){
        //Call the sum method of MethodTest
        MethodTest.sum(10,20);

        int j = 11;
        int k = 12;
        MethodTest.sum(j,k);
    }

    //Custom method
    public static void sum(int i,int j){
        System.out.println(i+"+"+j+"="+(i+j));
    }
}

  Method is not necessarily called in the main method

public class MethodTest02 {

    //Method is not necessarily called in the main method
    public static void main(String[] args){
        MethodTest02.sum(10,20);//Call sum() function
        System.out.println("HELLO");

    }
    public static void sum(int i,int j){
        System.out.println(i+"+"+j+"="+(i+j));

        //Call the dosome() method
        MethodTest02.dosome();
    }
    public static void dosome(){
        System.out.println("do some !");
    }

}

Study the return statement:

public class Return {
    //return once executed, the method ends
    //In the same method, no code is written under return. Because the following code cannot be executed, an error is reported during compilation

    //The following programs cannot be 100% compiled. return 1: compilation reports an error
    public static int m(){
        int a = 10;
        if (a > 3){
            return 1;
        }
    }

}
public static int m(){
        int a = 10;
        if (a > 3){
            return 1;
            System.out.println("Hello");  //return: code cannot be written after this. If it cannot be executed, an error will be reported during compilation
        }
        return 0;
    }

return is to end the entire method

break is to end the whole cycle. The scope of action of both is different

Method to perform memory analysis

During the execution of the method, how is the memory allocated in the JVM and how does the memory change?

1. The method is only defined, not called, and not executed. The JVM will not allocate the memory space of "run to" for the method, and the space will be allocated only when calling.

2. In the memory partition of the JVM, there are three memory spaces to pay attention to

        Method area memory

        Heap memory

        Stack memory

3. Stack, a data structure, which reflects the storage form of data. Common data structures: array, queue, stack, linked list, binary tree, hash table / hash table

The stack frame always points to the top of the stack

The top element of the stack is active and the other elements are static

Terminology: push / push   push

           Pop up / out pop

4. The method code fragment is a part of the. Class bytecode file. When the class is loaded, the bytecode is put into the method area. Therefore, among the three main memory spaces in the JVM, the method area first has data and stores the code fragment; although there is only one code fragment in the method area, it can be called repeatedly. Each time this method is called, the To allocate an independent activity place for this method, allocate it in the stack memory. [allocate the memory space to which the method runs in the stack memory]

5. When a method is called, it will allocate memory space to the method. At this time, the stack pressing action will occur. After the method is executed, all the memory space allocated to the method will be released, and the stack bouncing occurs

6. Local variables are declared in the method body. Local variables are stored in the stack during the running phase.

The parameter passes the value of the variable. Analyze the following code

public static void main(String[] args){
        int i = 10;
        method(i);
        System.out.println("main------"+i);
    }

    private static void method(int i) {
        i++;
        System.out.println("method-----"+i);
    }

The result is

  as a result of

overload of method

public class Overload01 {
    public static void main(String[] args){

        //Different parameter types lead to different methods to be called. At this time, the distinguishing method no longer depends on the method name, but on the type of parameter
        System.out.println(sum(1,1));
        System.out.println(sum(1l,1l));
        System.out.println(sum(1.0,1.0));
    }

    public static int sum(int i, int j) {
        System.out.println("sum(int, int)");
        return i+j;
    }
    public static long sum(long i, long j) {
        System.out.println("sum(long, long)");
        return i+j;
    }
    public static double sum(double i, double j) {
        System.out.println("sum(double, double)");
        return i+j;
    }
}

  It is more convenient for programmers to call methods. Although they call different methods, it feels like calling the same method without remembering more method names.

Beautiful code

Premise: when the functions are similar, the method names can be the same

However, when the functions are different, the method names should be as different as possible

When to consider method overloading?

When the functions are similar, try to make the method names the same, but when they are different or different, try to make the method names different.

What constitutes a method overload when conditions are met?

1 in the same class

2. The method name is the same

3. Different parameter lists: different quantity; different order; different type

    public static void m1(){}
    public static void mi(int a){}

    public static void m1(int a,double b){}
    public static void m1(double a,int b){}

    public static void m1(int a){}
    public static void m1(double a){}

What does method overloading have to do with? What does it have to do with?

Method overloading has nothing to do with the return value type and modifier list. Method overloading has something to do with the method name and parameter list

Method recursion

1 the method itself calls itself.

A(){

        A(){}

}

2 method recursion consumes stack memory, so try not to use it

3 an Error occurred when the following program was running [not an exception, but an Error]

java.lang.StackOverflowError    Stack memory overflow error

The error occurs and cannot be undone. The only result is that the JVM stops working

4 recursion must have an end condition, otherwise a memory overflow error will occur

5 even if the recursion has an end condition. Even if the end condition is correct, the stack memory overflow error may occur because the recursion is too deep

public class Recursion {
    //Summation of 1~n; recursion

    public static void main(String[] args){
        int i = 4;
        int result = sum(i);
        System.out.println(result);
    }
    public static int sum(int n){

        if (n == 0){
            return 1;
        }
        return n + sum(n-1);
    }

}

 

Posted by sell-traffic on Sat, 25 Sep 2021 19:13:46 -0700