Talk about the difference between final, finally, finalize

1. final 

  In Java, final can be used to modify classes, methods and variables (member variables or local variables). It will be described in detail below.

1.1 Modification class

  When a class is modified with final, it indicates that the class cannot be inherited by other classes. When we need to make a class never be inherited, we can use final modification at this time, but we should pay attention to:

All member methods in the final class are implicitly defined as final methods.

1.2 Modification method

There are two main reasons for using the final method:

  (1) Lock the method to prevent the inherited class from changing it.

  (2) Efficiency, in the early java version, the final method will be converted to inline calls. But if the method is too large, there may not be much improvement in performance. Therefore, in recent versions, no final method is required for these optimizations.

The final method means "last, final" meaning, that is, this method cannot be overridden.

Note: If the access permission of the final method in the parent class is private, the method cannot be directly inherited in the subclass. Therefore, at this time, a function with the same method name can be defined in the subclass. This will not be the same as the overriding final Contradiction, but redefine the new method in the subclass.

Copy code

class A{
    private final void getName(){

    }
}

public class B extends A{
    public void getName(){

    }

    public static void main(String[]args){
        System.out.println("OK");
    }
}

Copy code

  

1.3 Modified variables

   The final member variable represents a constant and can only be assigned once, and its value will not change after assignment. Similar to const in C++.

  When final modifies a basic data type, it means that the value of the basic data type cannot change after initialization; if final modifies a reference type, it can no longer point to other objects after its initialization, but The content of the object pointed to by the reference can change. It is essentially the same thing, because the referenced value is an address, and final requires a value, that is, the value of the address does not change. 

  Final modifies a member variable (attribute), which must be displayed and initialized. There are two initialization methods , one is to initialize when the variable is declared; the second method is to not assign an initial value when declaring the variable, but to assign this variable in all the constructors of the class where the variable is located Initial value.

 

  When the parameter type of the function is declared as final, it means that the parameter is read-only. That is, you can read and use the parameter, but you cannot change the value of the parameter.

 

   

 

  In Java, String is designed as a final class, so why can the value of String be changed in normal use?

  The string constant pool is a special storage area in the java heap memory. When we create a String object, assuming that the string does not exist in the constant pool, we will create one, and if it does, we will directly refer to the existing string. When we change the value of the String object, such as String a="A"; a="B". a is a reference to a String object (the String object we are talking about here actually refers to a string constant). When a="B" is executed, the original String object ("A") is not changed, but a new one is created Object ("B"), let a refer to it.

2. finally

   Finally, as a part of exception handling, it can only be used in try/catch statements, and a statement block is attached, which means that this statement will eventually be executed (regardless of whether an exception is thrown), and is often used when resources need to be released. Case. (×) (This sentence actually has certain problems)

  Many people think that the finally block will be executed, but is this really the case? The answer is no, such as the following example:

  

 

  When we remove the commented three-line statement, the execution result is:

  

   Why is the finally statement not executed in the above two cases? What is the problem?

  Only when the try statement block corresponding to finally is executed, the finally statement block will be executed. In the above two cases, an exception has been returned or thrown before the try statement block is executed, so the finally statement corresponding to the try is not executed.

  However, in some cases, even if the try statement is executed, the finally statement is not necessarily executed. For example:

  

  The finally block is still not executed, why? Because we executed the System.exit (0) statement in the try block, the Java virtual machine was terminated. Someone said that the System.exit(0) method is basically not called in general Java applications. OK! No problem. If we don't call the System.exit(0) method, will the finally block be executed?

  Let everyone down again, the answer is still no. When a thread is interrupted or terminated while executing a try block or catch block, the corresponding finally block may not be executed. There is a more extreme situation, that is, when the thread is running a try block or a catch block, it suddenly crashes or loses power, and the finally block will definitely not be executed. Some people may think that the reasons such as crashes and power outages are a bit irrational. It doesn't matter. We are just to illustrate the problem.

 

Error-prone

  Execute the return statement in the try-catch-finally statement. We look at the following code:

  

  Answer: 4,4,4. why?

  First of all, the finally statement will be executed in the modified code. From the running results, the result of each return is 4 (that is, the finally statement), as if other return statements are blocked.

  The fact is true, because the finally usage is special, so the previous return statement will be cancelled and the code in the last finally block will continue to be executed. 

  

3. finalize  

  finalize() is defined in java.lang.Object, which means that every object has such a method. This method is called when gc is started and the object is recycled. In fact, gc can recycle most of the objects (any new objects can be handled by gc, under normal circumstances we don't use methods other than new to create objects), so programmers are generally not required to implement finalize. 
In special cases, programmers need to implement finalize. When the object is recycled, some resources are released. For example, a socket link is created when the object is initialized and is valid for the entire life cycle. Then you need to implement finalize and close the link. 
  One more thing to note when using finalize is to call super.finalize();

  The finalize() method of an object will only be called once, and the finalize() being called does not mean that gc will immediately reclaim the object, so it is possible that after finalize() is called, the object does not need to be reclaimed, and then the real When it is to be recycled, because it has been called once before, finalize() will not be called, which causes problems. Therefore, it is recommended not to use the finalize() method, which is different from the destructor.

Guess you like

Origin blog.csdn.net/A___B___C/article/details/114029246