Difference and usage of final, finally, and finalize in Java, finalfinalize
1. Simple differences:
Final is used to declare attributes. Methods and classes indicate that attributes cannot be exchanged, methods cannot be overwritten, and classes cannot be inherited.
Finally is a part of the structure of the exception handling statement, indicating that it is always executed.
Finalize is a method of the Object class. When the garbage collector is executed, this method of the recycled Object is called for recycling other resources during garbage collection, such as closing files.
2. Moderate differences:
Although this word exists in Java, there is not much association:
Final: keyword in java, modifier.
A) if A class is declared as final, it means that it cannot generate A new subclass and cannot be inherited as A parent class. Therefore, a class cannot be declared as both abstract class and final class.
B) If variables or methods are declared as final, they are not changed during use.
1) variables declared as final must be declared with an initial value, which can only be read and cannot be modified in future references.
2) The declared final method can only be used and cannot be overloaded.
Finally: An Exception Handling Mechanism in java.
Finally is the best supplement to the Java Exception Handling Model. The finally structure enables the code to always be executed, regardless of the occurrence of no exception. Using finally, you can maintain the internal status of an object and clear non-memory resources. Especially in the aspect of closing the database connection, if the programmer puts the close () method of the database connection in finally, it will greatly reduce the chance of program errors.
Finalize: A method name in Java.
Java uses the finalize () method to clear objects from the memory before the Garbage Collector clears them. This method is called by the garbage collector when it determines that this object is not referenced. It is defined in the Object class, so all classes inherit it. Subclass overwrites the finalize () method to sort system resources or perform other cleanup tasks. The finalize () method is called before the Garbage Collector deletes an object.
3. Detailed differences:
This is another classic interview question. We can almost see it in the interview questions of various companies. Although final, finally, and finalize look like twins, their meanings and usage are quite different.
The final keyword is called final first. It can be used in the following four places:
1) define variables, including static and non-static variables.
2) define the parameters of the method.
3) define a method.
4). Define the class.
Define Variables, Including static and non-static. Define method parameters
First case:
If final modifies a basic type, it indicates that the value assigned to the variable is immutable, that is, it is a constant;
If final modifies an object, it indicates that the referenced variable is immutable.
Note that the reference stored in this variable is not the object pointed to by this reference.
Case 2: the meaning of final is the same as that of Case 1.
In fact, for the first two cases, a more appropriate description of the meaning of final, that is, if a variable or method parameter is modified by final, it means that it can only be assigned once, however, the default value set by the Java Virtual Machine for the variable is not recorded as a value assignment. Variables modified by final must be initialized. The initialization method is as follows:
1. Initialization during definition.
2. final variables can be initialized in the initialization block, but cannot be initialized in the static initialization block.
3. Static final variables can be initialized during definition or in static initialization blocks.
4. final variables can also be initialized in the class constructor, but static final variables cannot.
The following code verifies the above points:
Public class FinalTest {public final int A = 10; // initialize public final int B; {B = 20 ;} // initialization in the initialization block // non-static final variables cannot be initialized in the static initialization block // public final int C; static {// C = 30 ;} // static constant. initialize public static final int STATIC_D = 40 when defining; // static constant. initialize public static final int STATIC_E in the static initialization block; static {STATIC_E = 50;} // static variables cannot be initialized in the initialization block // public static final int STATIC_F; {STATIC_F = 60;} public final int G; // static final variables cannot be initialized in the constructor // public static final int STATIC_H; // public finalTest () {G = 70 is initialized in the constructor; // static final variables cannot be initialized in the constructor // STATIC_H = 80; // when the final variable is assigned A value for the second time, an error will be reported during compilation // A = 99; // STATIC_D = 99;} // if the final variable is not initialized, an error is reported during compilation. // public final int L; // The static final variable is not initialized, an error is reported during compilation. // public static final int STATIC_J ;}
After running the above code, we can find final variables (constants and static final variables (errors are reported when static constants are initialized.
Variables modified with final (constants are more efficient than non-final variables (normal variables have higher efficiency, so we should replace them with as many constants as possible in interprogramming.
Definition Method
When final is used to define a method, it indicates that this method cannot be overwritten by the quilt class, but it does not affect the inheritance of the quilt class. Let's write a code segment to verify it:
Public class ParentClass {public final void TestFinal () {System. out. println ("parent class -- this is a final method");} public class SubClass extends ParentClass {// The SubClass cannot be overwritten (the final method of the override parent class, otherwise, an error/* public void TestFinal () {System. out. println ("SubClass -- override final method");} */public static void main (String [] args) {SubClass SC = new SubClass (); SC. testFinal ();}}
Here, we need to note that methods with private access permissions can also be modified using final, but the subclass cannot inherit the private method, so it cannot be rewritten. The compiler treats the private method according to the final method, which improves the efficiency of the method when it is called. However, subclass can still define methods with the same structure as the private method in the parent class, but this does not produce the effect of rewriting, and there is no necessary link between them.
Definition class
Finally, let's review the usage of final for classes. We should be familiar with this because the most common String class is final. Because the final class cannot be inherited, the compiler treats all its methods as final during processing, so the final class has a higher efficiency than the ordinary class. Abstract classes defined by the keyword abstract contain abstract methods that must be implemented by the subclass that inherits them. Therefore, the same class cannot be modified by both final and abstract classes. In the same way,
Final cannot be used to modify interfaces. The methods of the final class cannot be overwritten, but this does not indicate the attributes of the final class (the variable value cannot be changed, and the attribute value of the final class cannot be changed, you must add final modifier to it. See the following example:
Public final class FinalTest {int I = 20; final int j = 50; public static void main (String [] args) {FinalTest ft = new FinalTest (); ft. I = 99;/* The FinalTest attribute value of the final class I can be changed because the final attribute value I is not mentioned before * // ft. j = 49; // error .... because j attributes are final and cannot be changed. System. out. println (ft. I );}}
Run the above Code and try again. The result is 99 instead of 10 during initialization.
Finally statement
Next, let's review the usage of finally. Finally can only be used in a try/catch statement with a block, indicating that the statement is always executed. See the following code:
Public final class FinallyTest {public static void main (String [] args) {try {throw new NullPointerException ();} catch (NullPointerException e) {System. out. println ("an exception thrown by the program");} finally {// This will always be executed, not affected by break. Another example is the database connection close (), which is generally written here, can reduce the probability of program errors System. out. println ("executed finally statement block ");}}}
The running result illustrates the role of finally:
1. The program throws an exception.
2. when the finally statement block is executed, it is not a good programming habit to capture the exceptions thrown by the program, neither to handle them nor to throw them up, it masks errors in program execution. It is only for demonstration purposes. Please do not learn.
In this case, the finally statement block cannot be executed?
Return, continue, and break can disrupt the statements executed in the code sequence. Let's try to see if this statement can affect the execution of finally statement blocks:
Public final class FinallyTest {// test return Statement // The result shows that when the compiler compiles return new ReturnClass ();, // It is divided into two steps: new ReturnClass () and return. The statement for creating the previous object is executed before the finally statement block, // The other return statement is executed after the finally statement block, that is to say, the finally statement block is the public ReturnClass testReturn () {try {return new ReturnClass ();} catch (Exception e) {e. printStackTrace ();} finally {System. out. println ("finally statement executed");} return null;} // test the continue statement public void testContinue () {for (int I = 0; I <3; I ++) {try {System. out. println (I); if (I = 1) {System. out. println ("con") ;}} catch (Exception e) {e. printStackTrace ();} finally {System. out. println ("finally statement executed") ;}}// test the break statement public void testBreak () {for (int I = 0; I <3; I ++) {try {System. out. println (I); if (I = 1) {break ;}} catch (Exception e) {e. printStackTrace ();} finally {System. out. println ("finally statement executed") ;}} public static void main (String [] args) {FinallyTest ft = new FinallyTest (); // test the return statement ft. testReturn (); System. out. println (); // test the continue statement ft. testContinue (); System. out. println (); // test the break statement ft. testBreak () ;}} class ReturnClass {public ReturnClass () {System. out. println ("return Statement executed ");}}
The running result of the above Code is as follows:
The return Statement is executed. The finally statement 0 is executed. The finally statement 1con is executed. The finally Statement 2 is executed. The finally statement 0 is executed. The finally Statement 1 is executed.
Obviously, return, continue, and break fail to prevent the execution of finally statement blocks. From the output results, the return statement seems to have been executed before the finally statement block. Is that true? Let's take a look at what is the role of the return Statement? Is to exit the current method and return the value or object. If the finally statement block is executed after the return statement, the return Statement has exited the current method after it is executed. How can the finally statement block be executed? Therefore, the correct execution sequence should be like this: when the compiler compiles return new ReturnClass ();, it is divided into two steps: new ReturnClass () and return, the statement of the previous object is executed before the finally statement block, and the return Statement is executed after the finally statement block, that is to say, the finally statement block is executed before the program exits. Similarly, the finally statement block is executed before the loop is skipped (continue and interrupt (break)
Finalize method
Finally, let's take a look at finalize, which is a method belonging to java. lang. object class, which is defined as follows: protected void finalize () throws Throwable {} as we all know, the finalize () method is part of the GC (garbagecollector operating mechanism. Here we only talk about finalize () what is the role of a method? The finalize () method is called when GC clears the objects it belongs to. If an uncaught exception (uncaughtexception, GC will terminate the cleaning of the modified object and the exception will be ignored. Its finalize () will be called again until the next GC begins to clean the object. See the following example:
Public final class FinallyTest {// override the finalize () method protected void finalize () throws Throwable {System. out. println ("The finalize () method is executed");} public static void main (String [] args) {FinallyTest ft = new FinallyTest (); ft = null; System. gc ();}}
The running result is as follows: • The finalize () method is executed.
The program calls java. lang. the gc () method of the System class causes GC execution. GC calls its finalize () method when clearing the ft object, so the above output result is obtained. Call System. gc () is equivalent to calling the following line of code: Runtime. getRuntime (). gc (); the function of calling them is only to recommend the Garbage Collector (GC startup, clearing useless objects to release memory space, but GC startup is not certain, which is determined by the Java Virtual Machine. Until the Java Virtual Machine stops running, the finalize () of some objects may not have been run. So how can we ensure that this method of the object will be called before the Java Virtual Machine stops running? The answer is that we can call another method of the System class:
public static void FunFinalizersOnExit(boolean value){ //othercode}
If this method is set to true, the finalize () method of the object must be run before the Java Virtual Machine stops running. Unfortunately, this method is not safe, it will cause the useful object finalize () to be called by mistake, so it is no longer in favor of use. Because finalize () belongs to the Object class, all classes use this method, and any subclass of the Object can be rewritten (override this method, in which system resources are released or other cleanup work is performed, for example, disable input/output streams. Through the review of the above knowledge, I think the usage differences between final, finally, and finalize are clear.