the difference between final,finally and finalize
I. GENERAL differences
Final is used to declare properties, methods, and classes that indicate that the property is not mutable, that the method cannot be overridden, and that the class cannot be inherited.
Finally is the exception-handling statement structure that represents the part that is always executed.
A finallize representation is a method of the object class that is invoked when it is executed in the garbage collection mechanism. Allows recycling of memory garbage that was not previously reclaimed. All of the object inherits the Finalize () method
two. Final detailed
Final in Java is also used to modify 3 kinds of things: variables, methods, and classes.
1. Variable
The final modifier variable indicates that the variable is immutable. For example, final int i = 1; the value of I in the program is not allowed to change. It is easy to confuse that when final is used to decorate a reference variable, it means that the reference variable is immutable, that is, the memory address to which the reference variable points is invariant, but the class in the memory address being referred to can be changed. For example:
Final MyClass MyClass = new MyClass ();
When you declare MyClass, the memory address that it points to is fixed, but you can still change the member variable of the object referenced by MyClass. If you try to reuse MyClass this variable to refer to another object, you will get an error.
MyClass = new MyClass ();//ERROR!!!
2. Methods
The final cosmetic method indicates that the method cannot be overridden by a quilt class.
3. Class
The final modifier class indicates that the class is not inherited, and because of the single inheritance relationship of Java, the class is a terminal in the inheritance chain.
A few things to note about final:
A, final variables must be initialized at the time of declaration or initialized in constructors;
b, all the variables declared in the interface are final;
c, the difference between final,finally,finalize. Final presentation is not variable, final represents the statement that must be executed, and finalize the code that executes when garbage collection is collected.
three. Finally detailed
Java exception handling model in contrast to other languages, the keyword finally is the most outstanding new feature. The finally widget makes the code in the section always execute, regardless of whether an exception occurs, especially for maintaining the internal state of the object (used to ensure that the exception is in a valid state for recovering the object). To ensure that the program will automatically run again after processing the exception and to clean up non-memory resources (resources that the garbage collection mechanism cannot handle, such as database connections, sockets, and so on).
One thing to note, though, is to try not to return from the try section (call back) because as long as a finally section exists, it is bound to be executed, and if you call a return statement in the finally section, the returned value in the Try section is obscured. So that the method caller gets the return value in the finally section-which is often the original intention of the program writing.