Java Interview Question 1: The difference and connection of the three keywords of final, finally, and finalize

1、final:

    Final can modify classes, variables, and methods.

    Final modified classes cannot be inherited, because abstract classes are used for inheritance, so abstract and final keywords cannot coexist.

    When final modifies a class, it means that the class cannot be inherited, final cannot modify the interface, and final modification method means that the method cannot be overridden. When final modifies a variable, it must be assigned when affirming, indicating that the variable is a constant and cannot be Re-assign.

2、finally:

    Finally is generally used for the release of resources, such as closing the database connection. It is used in the exception try-catch-finally. When handling exceptions, we usually put the code we must execute in the finally code block, regardless of whether the exception occurs or not. , The code in finally will be executed.

But the code in finally will not necessarily be executed, and the code in finally will not be executed in the following cases:

     ① An exception occurred before entering the try code block

     ② Call System.exit(0) in the try code block to terminate the operation of the virtual machine

     ③ The program is interrupted in try block or catch block, such as crash

Example 1:

public class Demo02 {
public static void main(String[] args) {
    System.out.println(methd());
}
public static int methd(){
    int num = 1;
    try{
        return num;
    }catch (Exception ex){
        return 0;
    }finally {
        num++;
    }
    }
}

//程序运行的结果为:1

  Why is the result of running the above program 1?

  Let's think about it, should the printed method() method print 1 or 2? Is the code in finally executed before return or after finally? When the execution of the try code block is executed, the return num value 1 will first be stored on the stack. At this time, the program does not return the value, but executes num++, the value of num++ becomes 2, and then the program returns to the end. The time method still returns 1 in the stack.

Example 2:

public classDemo02 {
public static voidmain(String[] args) {
    System.out.println(methd());
}
public static intmethd(){
    int num = 1;
    try{
        return num;
    }catch(Exception ex){
        return 0;
    }finally{
        return 2;
    }
    }
}

Everyone, guess whether the result returned by this example is 1 or 2? To analyze, first the try code block is executed, the value of num 1 is stored in the stack, and then the finally code block is executed, the return value is 2, and 2 is stored in the stack, replacing the 1 in the stack, so at the end The result is 2.

to sum up:

1. Finally, after the try block and catch block return are executed, they are executed before returning, and the returned value is the previous one.

2. If there is no return in finally, the execution result does not affect the determined return value in try and catch.

3. If there is return in finally, the execution result will be returned directly.

3、finalize

   Finalize is a method that belongs to the Object class. The finalize() method is called when the GC cleans up the object it belongs to. The Object class is the parent class of all classes. The finalize() method is generally called by the garbage collector. When garbage gc() is called to collect garbage, the garbage collector calls finalize() to collect garbage.

 

Guess you like

Origin blog.csdn.net/Sunshineoe/article/details/114604429