The role of final in Java

Source: Internet
Author: User

1. Finishing the final of the underlying data member : This is the primary purpose of final , meaning the equivalent of C/s + + const, that is, the member is decorated as a constant, means that it cannot be modified. If PI and E in the Java.lang.Math class are final members, the value is 3.141592653589793 and 2.718281828459045.

2. Modifying a reference to a class or objectFinal: InJava, we cannot let the object be decorated asFinalAnd can only modify the object's reference, which means that even if you writepublic final A A = New a ();   In fact a the data of the object pointed to can still be modified, cannot be modified by the Span lang= "en-US" >a The reference value of the itself, i.e. you can no longer a for re-assignment. The same situation appears in the array, such as public final int[] A = {1, 2 , 3, 4, 5} , in fact a are modifiable, That is, you can write a[0] = 3 . According to the current understanding, java in the array of data can not be decorated as non-modifiable, and c/c++ yes.

3. The final of the adornment method: The final of the adornment method and the const of the decorated member object are very different in C/C + + . First, the final meaning of the adornment method is not "non-modifiable", but the method cannot be redefined by the inherited member. (Note that what is said here cannot be redefined, not that the subclass must not define a method of the same name, if the parent class's method is a private type, the subclass is allowed to define the method, and here refers to the inability to redefine the method to make the method rewrite the polymorphism can be achieved, if you do not want A A = new B (); A.F (); Such an overriding method occurs)

Example:

public class A {

Final method F

Public final void F () {

System. out. println ("Final method F in Class A is called");

}

}

public class B extends A {

Compile Error! The F method of the parent class is the final type and cannot be overridden!

//! public void F () {

//! System. out. println ("Method F in Class B is called");

//! }

}

In addition, when a method is decorated as a final method, it means that the compiler may load the method inline (inline), which means copy into the original code. This allows the code to execute faster (because the cost of calling the function is omitted), such as in int[] arr = new Int[3] Call arr.length () , and so on. On the other hand, private methods are also implicitly decorated by the compiler as final , which means private final void F () and private void F () There is no difference between the .

inline invocation in Java: method Inline is the code "copy" of the target method into the method that initiates the call, avoids the real method call, when the compiler is inline, if it is a non-virtual method, then inline directly, if the virtual method is encountered (call with invokevirtual), Java by introducing the "Type Inheritance Relationship Analysis" (Class Hierarchy, cha), Cha queries Whether this method has multiple target versions available under the current program, and if the query results have only one version, it can be inline, called the daemon inline, If the CHA query obtains multiple versions of the target method, the compiler uses an inline cache to complete the method inline: the inline cache state is empty before the method call occurs, and when the first call occurs, the cache records the version information of the receiver of the method and compares the recipient version each time the method call is made. If the method receiver version of each call that comes in later will continue to use inline, and if a change occurs, the program uses the polymorphic nature of the virtual method, it cancels the inline and finds the virtual method for the method's dispatch.

4. Final of the modified class : When a class is decorated to final , it is very clear that the class is not allowed to be inherited, that is, the class is "unrepeatable", and any operations that inherit it will end up with a compilation error. This also underscores the reason that Java uses final instead of const as an identifier.

The role of final in Java

Related Article

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.