Simple talk about the difference of final,finally,finalize in Java _java

Source: Internet
Author: User

(1) Final: modifier (keyword), if a class is declared final, means that it can no longer derive a new subclass and cannot be inherited as a parent class. Thus a class cannot be declared as abstract and declared final. Declaring a variable or method final can guarantee that they will not be changed in use. A variable declared as final must be given an initial value at the time of declaration, and can only be read and not modified in future references. A method that is declared final is also used only and cannot overload

(2) Finally: Provides a finally block in exception handling to perform any cleanup operations. If an exception is thrown, the matching catch clause executes, and then the control enters the finally block (if any).

(3) Finalize: Method name. Java technology allows you to use the Finalize () method to do the necessary cleanup before the garbage collector clears the object out of memory. This method is invoked by the garbage collector on this object when it is determined that the object is not referenced. It is defined in the Object class, so all of the classes inherit it. Subclasses override the Finalize () method to defragment system resources or perform other cleanup work. The Finalize () method is invoked on this object before the garbage collector deletes the object.

It may be a little vague, and we'll go into it in more depth.

The nature of a different

(1) Final is the key word;

(2) Finalize () as the method;

(3) Finally is the block mark, used in the try statement;

Second, the role

(1) Final is the keyword used to identify the constant, and the final identified keyword is stored in the constant pool (where the final constants are described below);

(2) The Finalize () method is defined in object to be invoked by the JVM for garbage collection of objects, similar to destructors in C + +, when the object "disappears", and is used to release the resource occupied by the object (for example, for i/0 operations) when the user customizes it;

(3) finally{} is used to identify a block of code that is compatible with try{}, regardless of whether the code in the try is finished or not executed (here is an exception), the program in the code block is bound to occur;

Third, final detailed

1 Defining variables

1.1 Final defines the base type variable, the requirement that the variable initialization must not be used elsewhere in the declaration or in the constructor. Constants defined by this keyword that cannot change the value of a constant in addition to the initialization phase.

1.2 A reference to the final definition object that is initialized in accordance with the requirements for defining constants; The content of the object defined by the keyword can be changed, but the address pointed to by the reference cannot be changed;

2 Defining parameters

If you pass in the variable defined by this parameter, method cannot modify (error) the content of the parameter, the same as the modification rule that defines the variable; Java methods pass a value when the base type is passed, and the;< of the object is the passing of the reference. In the final analysis, the delivery of a method in Java is dependent on passing "copy ": For the base type, first create a copy, assign the value passed to copy, and then manipulate copy; For the object type, first establish a reference copy and assign the incoming object reference to the Copy>

For example: Method (final int test);

Some books say that the final definition of parameters, especially the parameters of the object is very useful, not in the method for the content of the object to change, such a statement is wrong! I also think that some of the features of functional programming, can not modify the content of the object, here can still modify the contents of the object.

?? What's the use of defining this parameter??

String is a born final type!

3 Definition Method

(1) Using the final keyword definition method, can not inherit the quilt class;

(2) allows the compiler to convert all calls to this method into inline (inline) behavior, that is, this method can be copied directly at the call, rather than routine method calls (save breakpoints, stack), which can increase the efficiency of the program. But---------if too much, this will cause code expansion, but will affect efficiency, so the method should be used with caution.

4 Definition Class

Any final class cannot be inherited by anyone, which means that this class is a leaf class in an inheritance tree, and that this class is considered perfect and does not require any modification (in short, it is not recommended).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.