This is the sixth day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Important note: This article is part of a series of interview questions. Please follow me to check out the series of interview articles. Gitee open Source address: gitee.com/mydb/interv…

The difficulty of this topic: low

Common degree: High

1. The final is introduced

Final is a keyword in the Java language, and objects decorated with final are not allowed to modify or replace their original values or definitions.

Final can be used to modify classes, methods, variables, and parameters. It can also be used to modify the term “parameter”, which is easy to forget. These are the four uses of final.

1.1 Final Usage Instructions

  • When final decorates a class, it is not allowed to be inherited, indicating that the class is perfectly designed and does not need to be modified or extended.
  • When final decorates a method, it does not allow any classes that inherit from that class to override the method, indicating that the functionality provided by the method meets the current requirements and does not need to be extended.
  • When final decorates a variable, it means that the variable cannot be modified once it has been initialized.
  • When final decorates a parameter, it means that the parameter is not allowed to be modified throughout the method.

1.2 Final Usage demonstration

Final modifiers:

final class Animal {}Copy the code

Final decoration methods:

public class FinalExample {
    public final void sayHi(a) {
        System.out.println("Hi~"); }}Copy the code

Final modifies variables:

public class FinalExample {
    private static final String MSG = "hello";
	/ /...
}
Copy the code

Final modifier parameter:

public class FinalExample {
    public void sayHi(final String name) {
        System.out.println("Hi,"+ name); }}Copy the code

2. The finally is introduced

Finally is a mechanism in Java to ensure that important code must be executed.

We can use try-finally or try-catch-finally to do things like close a JDBC connection and ensure that a lock is released.

2.1 Finally Usage Demonstration

try {
    // do something
} finally {
    // The code that must be executed
}
Copy the code

2.2 the finally extended

Sometimes the interview question is: Will finally be executed? Finally will always be executed, but there is a special case where finally will not be executed. The special implementation code and execution result are as follows:

3. Finalize is introduced

Finalize is a basic method in the Object class. It is designed to ensure that objects are collected for certain resources before being garbage collected, but it has been marked deprecated in JDK 9.

Finalize method is not recommended in practical development. It is created, but there is no guarantee that Finalize method will be executed, so do not rely on it to release any resources, because its execution is extremely “stable”. Deprecating it in JDK 9 proves the point.

Finalize Performance Problems

Finalize has some performance problems besides “unstable” execution.

Because the execution of Finalize is associated with garbage collection, once the non-empty Finalize method is implemented, the order of magnitude of object collection presentation will be slow. Someone has made a special benchmark, which is about 40~50 times lower.

Because Finalize is designed to be called before objects are garbage collected, it means that the object that implements finalize is a “special citizen” that the JVM will do extra processing to. Finalize is essentially an obstacle to fast collection, which can cause your objects to go through multiple garbage collection cycles before being collected.

conclusion

Final, finally, and Finalize seem to be very similar from the literal perspective of English, but actually they have no relation in Java. Final is the keyword used to modify classes, methods, variables, and parameters. Objects modified by final are not allowed to modify or replace their original values or definitions. Finally is a mechanism in Java to ensure that important code must be executed; Finalize is a basic method in the Object class. It is designed to ensure that objects can complete the collection of specific resources before being garbage collected. However, finalize execution is “unstable” and has certain performance problems, and has been set as deprecated in JDK 9.

Reference & acknowledgements

Java Core Technology Interview