In-depth understanding of the final keyword in Java

final is an important keyword in Java, it can be used in the classes, methods and variables. This article will explain what is the final keyword? The variables, methods and classes declared final represents what? What are the benefits of using final?

What final key is?

final is a reserved keyword in Java, you can declare member variables, methods, classes, and local variables. Once you declare the reference as final, you will not be able to change the references, the compiler will check the code, if you try to re-initialize a variable, then the compiler will report compilation errors.

final variables

Any member variables or local variables (or process variables in the code block is called a local variable) are declared as final called final variable. final and static variables often used with keywords, as a constant. Here is an example of final variables:

public static final String NAME = "wupx";
NAME = new String("wupx"); //invalid compilation error

final variable is read-only.

final method

The method can also be declared final, Java in a final qualifier to a modified method of use is the only correct expression: This method was originally a virtual method, this method does not allow to declare now further be overridden in a derived class (override) by final .

Java-Africa private member method is the default virtual methods, and virtual method can be overridden in a derived class. In order to ensure a virtual method on a class is not a derived class is further overwritten, you need to use the final modifier to declare, let the compiler (such as javac) Check with JVM and to ensure that this limit is always true.

Big R in the know on the answer to almost break "with the final modification methods allow faster calls to this method of" rumors following references:

There was a widely circulated statement is final modification methods can make calls to this method becomes faster. The opinion on the modern mainstream JVM optimization is not established (such as Oracle JDK / OpenJDK HotSpot VM, IBM J9 VM, Azul Systems Zing VM, etc.). This is because: use modified final virtual method, which derived classes to override the inevitable can not exist on its version, so you can determine the virtual method call is only one possible target; and if at this time to remove this final qualifier these are available through the advanced JVM "class hierarchy analysis" (class hierarchy analysis, CHA) to find that this method does not further overridden in a derived class version, so can also determine the virtual method call is only one possible target. Then both the degree of optimization will be exactly the same, whether it is from the "unnecessary and can be called directly by the target virtual dispatch" (referred to as "de-blurring", devirtualization) or in terms of "convenience inline" look, both of all the same.

If a class hierarchy originally a virtual method to overwrite multiple versions, then this would have been impossible for virtual method plus final modification, so even if this situation continues blur becomes difficult, but also pot can not let the "useless because the final modification" to the back.

With the final keyword in optimizing the mainstream of modern JVM will not improve performance.

The following are examples of final methods:

class User{
    public final String getName(){
        return "user:wupx";
    }
}

class Reader extends User{
    @Override
    public final String getName(){
        return "reader wupx"; //compilation error: overridden method is final
    }
}

final class

Use final to modify the class category called final, final class usually is fully functional, they can not be inherited, Java has many classes are final, such as String, Interger and other packaging. The following are examples of final class:

final class User{

}

class Reader extends User{  //compilation error: cannot inherit from final class

}

The final memory model

For the final variable, the compiler and processor reordering must abide by two rules:

  • Within the constructor, to write a final variable, and then to this configuration referenced object assigned to a variable, not between these two operations reordering
  • Object contains a first reading of the final variable, and then read the final for the first time variable, not reordering between the two operations

In fact these two rules is what writing and reading for final variables. Write reordering rules can guarantee that a reference to any of the previous thread is visible, final variable object has been initialized correctly, and ordinary variables not have this guarantee in the object; read reordering rules can guarantee that, before reading an object's final variable , you will first read this object. If you read the quote is not empty, write according to the above rules, instructions and final variable object of a certain initialization is complete, so you can read the correct variable values.

If the type of final variable is a reference type, then the constructor function, a final write to the object referenced by the member of a domain, and then this is referenced in configuration outside the object constructor a reference to the variable assignment, the two operations between can not reorder.

In fact, in order to assure final variable before visible to other threads can correct initialization is complete.

Benefits final keyword

Here are some benefits of using the final keyword:

  • final keyword improved performance, JVM and Java applications are cached final variable
  • final variables can safely be shared in a multithreaded environment, without the need for additional synchronization overhead

to sum up

  • The final keyword can be used for member variables, local variables, methods and classes
  • final member variables must be initialized or initialized in the constructor when it was declared, otherwise a compilation error reporting
  • Can not be assigned again for final variables
  • Local variables must be assigned at the time of declaration
  • In the anonymous class in all variables must be final variable
  • final methods can not be overridden
  • final class can not be inherited
  • All variables declared in the interface itself is final
  • final and abstract both keywords are inversely related, final class can not be abstract of
  • Uninitialized variables final declaration called upon the blank final variable (blank final variable), they must be initialized in the constructor, or by calling this () initialization, do not do this, the compiler will complain final variable (variable name) needs initialized
  • According to the Java code convention, final variable is constant, and usually constant names to uppercase
  • For a collection of objects declared as final means that reference can not be changed

reference

"Java programming ideas"

https://www.zhihu.com/question/66083114

file

Guess you like

Origin www.cnblogs.com/wupeixuan/p/11750053.html