The difference between final, finalize, and finally Java

Source: Internet
Author: User

One, the nature of different
(1) Final is the key word;
(2) Finalize () as the method;
(3) Finally is a block flag for use in a 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 concrete usage of the final constant is described below);
(2) The Finalize () method is defined in object to be invoked by the JVM when the object "disappears" for garbage collection of objects, similar to a destructor in C + +, which frees up resources used by objects (such as i/0 operations) when the user is customized;
(3) finally{} is used to identify a block of code, with try{}, regardless of whether the code in the try is finished or not executed (this means there is an exception), the code block of the program will be done;
Third, final detailed
1 Defining variables
1.1 Final when defining a primitive type variable, it is required that the initialization of the variable must be in the declaration or in the constructor, not elsewhere. Constants that are defined by the keyword cannot change the value of a constant in addition to the initialization phase.
1.2 Final defines the reference to the object, which is initialized with the requirements of defining constants, the object content defined by the keyword can be changed, but the address pointed to by the reference cannot be changed;
2 Defining parameters
If the variable defined by the parameter is passed in, the method cannot modify the contents of the parameter (error), the same as the modification rule of the definition variable, and the Java method passes the basic type when the value is passed, and the Java method passes the argument to the object;
< Ultimately, the delivery of a method in Java relies on passing a "copy": for the base type, first create a copy, assign the incoming value to copy, and then manipulate the copy, and for the object type, first establish a reference copy and assign the incoming object reference to the copy >
Example: Method (final int test);
Some books say that the final definition parameter, especially the object's argument, is very useful and cannot be changed within the method for the object's contents, which is wrong! I also think that some of the characteristics of functional programming, can not be modified to the content of the object, it is still possible to modify the contents of the object.
?? What is the use of defining this parameter??
String is naturally the final type!
3 Defining methods
(1) The method defined using the final keyword cannot be inherited by the quilt class;
(2) allows the compiler to convert all calls to this method into inline (inline) behavior, which means that this method can be copied directly at the call, rather than making routine method calls (saving breakpoints, compacting the stack), which increases the efficiency of the program. However---------if too much, this will cause code bloat, but will affect efficiency, so this method should be used with caution.
4 Defining classes
Any final class cannot be inherited by anyone, which means that the class is a leaf class in an inheritance tree, and this class is considered perfect and does not require any modification (in short, it is deprecated)

The difference between final, finalize, and finally Java

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.