In-depth analysis of final implementation principles in Java (with examples)
This article brings you an in-depth analysis of the final implementation principle in Java (with examples). It has certain reference value. Friends in need can refer to it. I hope it will be useful to you. Helps.
Final is a reserved keyword in Java that can declare member variables, methods, classes and local variables.
Once you make the reference declaration final, you will not be able to change the reference. The compiler will check the code. If you try to initialize the variable again, the compiler will report a compilation error.
1. Final variables
Final member variables represent constants and can only be assigned once. The value will not change after the assignment (final requires the address The value cannot be changed)
When final modifies a basic data type, it means that the value of the basic data type cannot change once it is initialized; if final modifies a reference type, it will change after it is initialized. It can no longer point to other objects, but the content of the object pointed to by the reference can change. Essentially it's the same thing, because the referenced value is an address, and final requires the value, that is, the value of the address not to change.
Final modifies a member variable (property) and must be initialized explicitly. There are two initialization methods. One is to initialize the variable when it is declared. The second method is to not assign an initial value when declaring the variable, but to assign it to the variable in all constructors of the class where the variable is located. initial value.
2. Final method
There are two reasons for using the final method.
The first reason is to lock the method to prevent any inherited class from modifying its meaning and cannot be overridden;
The second reason is efficiency. Final methods are faster than non-final methods. Fast, because it is statically bound during compilation and does not need to be dynamically bound at runtime.
(Note: The private method of the class will be implicitly designated as the final method)
3. Final class
When a class is modified with final, it indicates that this class cannot be inherited.
Member variables in the final class can be set to final as needed, but be aware that all member methods in the final class will be implicitly designated as final methods.
When using final to modify a class, you should choose carefully. Unless this class really will not be used for inheritance in the future or for security reasons, try not to design the class as a final class.
4. Summary of the use of final
Benefits of the final keyword:
(1) The final keyword improves performance. Both JVM and Java applications cache final variables.
(2) Final variables can be safely shared in a multi-threaded environment without additional synchronization overhead.
(3) Using the final keyword, the JVM will optimize methods, variables and classes.
Important knowledge points about final
1. The final keyword can be used for member variables, local variables, methods and classes.
2. Final member variables must be initialized when declared or initialized in the constructor, otherwise a compilation error will be reported.
3. You cannot assign a value to a final variable again.
4. Local variables must be assigned a value when declared.
5. All variables in anonymous classes must be final variables.
6. The final method cannot be overridden.
7. Final classes cannot be inherited.
8. The final keyword is different from the finally keyword, which is used for exception handling.
9. The final keyword is easily confused with the finalize() method. The latter is a method defined in the Object class and is called by the JVM before garbage collection.
10. All variables declared in the interface are final.
11. The two keywords final and abstract are anti-correlated, and the final class cannot be abstract.
12. The final method is bound during the compilation phase, which is called static binding.
13. Final variables that are not initialized when declared are called blank final variables. They must be initialized in the constructor or initialized by calling this(). If you don't do this, the compiler will report an error "final variable (variable name) needs to be initialized".
14. Declaring classes, methods, and variables as final can improve performance, so that the JVM has the opportunity to estimate and then optimize.
15. According to Java code conventions, final variables are constants, and usually constant names should be capitalized.
16. Declaring a collection object as final means that the reference cannot be changed, but you can add, delete or change content to it.
5. Final principle
It is best to first understand the java memory model Java concurrency (2): Java memory model
For the final domain, the compiler and processor must abide by two Reordering rules:
1. Writing a final field in the constructor and subsequently assigning a reference to the constructed object to a reference variable cannot be reordered between these two operations.
(Write the final variable first, then call the object reference)
Reason: The compiler will insert a StoreStore barrier after writing the final field
2. The first reading of a reference to an object containing a final field and the subsequent first reading of the final field cannot be reordered between the two operations.
(Read the reference of the object first, then read the final variable)
The compiler will insert a LoadLoad barrier in front of the read final field operation
Example 1:
public class FinalExample { int i; // 普通变量 final int j; // final 变量 static FinalExample obj; public void FinalExample() { // 构造函数 i = 1; // 写普通域 j = 2; // 写 final 域 } public static void writer() { // 写线程 A 执行 obj = new FinalExample(); } public static void reader() { // 读线程 B 执行 FinalExample object = obj; // 读对象引用 int a = object.i; // 读普通域 a=1或者a=0或者直接报错i没有初始化 int b = object.j; // 读 final域 b=2 } }
The first case: the operation of writing the ordinary field is reordered by the compiler outside the constructor
And the operation of writing the final field is written as final The field reordering rules are "limited" within the constructor, and reader thread B correctly reads the value of the final variable after initialization.
Writing the reordering rules of the final field can ensure that the object's final field has been correctly initialized before the object reference is visible to any thread, while ordinary fields do not have this guarantee.
The second case: the operation of reading the ordinary field of the object is reordered by the processor to read before reading the object reference
And read The reordering rules of the final field will "limit" the operation of reading the object's final field to after reading the object reference. At this time, the final field has been initialized by the A thread, which is a correct read operation.
The reordering rules for reading final fields ensure that before reading the final field of an object, the reference to the object containing the final field must be read first.
Example 2: If the final field is a reference type
For reference types, write the final field The reordering rules add the following constraints to the compiler and processor:
Write the member fields of a final referenced object within the constructor, and subsequently write the reference to the constructed object outside the constructor Assignment to a reference variable cannot be reordered between these two operations.
public class FinalReferenceExample { final int[] intArray; // final 是引用类型 static FinalReferenceExample obj; public FinalReferenceExample() { // 构造函数 intArray = new int[1]; // 1 intArray[0] = 1; // 2 } public static void writerOne() { // 写线程 A 执行 obj = new FinalReferenceExample(); // 3 } public static void writerTwo() { // 写线程 B 执行 obj.intArray[0] = 2; // 4 } public static void reader() { // 读线程 C 执行 if (obj != null) { // 5 int temp1 = obj.intArray[0]; // 6 temp1=1或者temp1=2,不可能等于0 } } }
Assume that first thread A executes the writerOne() method, after execution thread B executes writerTwo() method, and after execution thread C executes reader () method.
In the above figure, 1 is writing to the final field, and 2 is writing to the member field of the object referenced by this final field. , 3 is to assign the reference of the constructed object to a reference variable. In addition to the aforementioned 1 cannot be reordered with 3, 2 and 3 cannot be reordered either.
JMM can ensure that the reading thread C can at least see the writing of the member field of the final reference object by the writing thread A in the constructor. That is, C can at least see that the value of array index 0 is 1. The writing of array elements by writing thread B may or may not be visible to reading thread C. JMM does not guarantee that thread B's writing is visible to reading thread C, because there is data competition between writing thread B and reading thread C, and the execution results at this time are unpredictable.
The above is the detailed content of In-depth analysis of final implementation principles in Java (with examples). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

A constant variable is a variable whose value is fixed and only one copy exists in the program. Once you declare a constant variable and assign a value to it, you cannot change its value again throughout the program. Unlike other languages, Java does not directly support constants. However, you can still create a constant by declaring a variable static and final. Static - Once you declare a static variable, they will be loaded into memory at compile time, i.e. only one copy will be available. Final - Once you declare a final variable, its value cannot be modified. Therefore, you can create a constant in Java by declaring the instance variable as static and final. Example Demonstration classData{&am

The difference between final, finally, and finalize in Java requires specific code examples. In Java programming, you often encounter the three keywords final, finally, and finalize. Although they are spelled similarly, they have different meanings and usages. This article will explain the differences between these three keywords in detail and give code examples to help readers better understand. 1. Final keyword The final keyword can be used for classes, methods and variables. Its function is to make the modified class

In Java, final can be used to modify classes, methods and variables. The final modified class means that the class cannot be inherited by any other class, which means that this class is a leaf class in an inheritance tree, and the design of this class has been considered perfect and does not need to be modified or extended. The method in the final modified class means that the class cannot be inherited by any other class and cannot be overridden; that is, the method is locked to prevent the inherited class from changing it. final modifies a variable in a class, indicating that the variable cannot be changed once it is initialized.

There are two ways to create final objects in Java: declaring a final variable or declaring a class using the final modifier. When a final variable is declared, the object is created through an initializer; when a final class is declared, the class instance is immutable. Importantly, references to final objects can still change, but the objects they point to are immutable.

As a very popular programming language, Java is widely used in the Internet, mobile devices and other fields. For Java developers, they often encounter some errors or problems, one of which is the abuse of the final keyword. In Java, the final keyword is often used to modify variables, methods, classes, etc. It indicates that the attribute cannot be changed after it is defined. The final keyword can help developers ensure the immutability of objects and avoid problems such as race conditions, but misuse of the final keyword

PHP is a popular open source server-side scripting language widely used in web development. The PHP language is not only easy to learn and use, but also supports a variety of programming paradigms, object-oriented programming, functional programming, etc. In PHP, there are some special syntax keywords, such as Static, Final, Abstract, etc. These keywords have special functions in object-oriented programming. This article will introduce these keywords in detail. Static keyword In PHP, the Static keyword has two uses

The "final" keyword in Java can be used to define constant values and prevent variables, methods, or classes from being changed or overwritten. Immutability, on the other hand, describes the characteristic of an object remaining in a constant state throughout its existence. Once an object is formed, its value does not change. Variables, methods, and classes are restricted by the "final" keyword, but immutability goes a step further and ensures that the entire state of the object is preserved. Let us understand the key differences between final and immutable in this article. The final keyword in Java Final Edition Java has several characteristics: Final variable: Its initial value cannot be modified after initialization. They are often used to declare unchangeable or immutable values. Final methods: They cannot be modified by subclasses, ensuring that they behave consistently. they help

Whenever you declare a method as final, you cannot override it. That is, you cannot provide a subclass with an implementation of a superclass's final method. That is, the purpose of declaring a method as final is to prevent modification of the method from outside (subclasses). In inheritance, when you extend a class, the subclass inherits all members of the superclass except the constructor. In other words, constructors cannot be inherited in Java, therefore you cannot override a constructor. Therefore, there is no point in prefixing the constructor with final. Therefore, Java does not allow the use of final keyword before a constructor. If you try to declare a constructor final, a compile-time error will be generated saying "modifierf
