In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "what are the small details of the final keyword". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "what are the small details of the final keyword?"
Text
The final keyword is a commonly used keyword that modifies variables, methods, and classes to indicate that the classes, methods, and variables it modifies are immutable. Here are some small details of using the final keyword.
Details 1. Final modifies the timing of assignment of class member variables and instance member variables
For class variables:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
The initial value is assigned directly when the variable is declared
Assign an initial value to a class variable in a static code block
The following code is shown:
The public class FinalTest {/ / a variable directly assigns the value private final static int a = 1; the private final static int b; / / b variable assigns the value static {bread2;}} through the static code block
For instance variables:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
Assign values directly when declaring variables
Assign values in non-static code blocks
Assign initialization values in the constructor
The following code is shown:
The public class FinalTest {/ / c variable directly assigns the value private final int c = 1 when declared; the private final int d; private final int e; / / d variable assigns the value {diter2; the / e variable assigns the value FinalTest () {ethere3;}} in the constructor.
Detail 2. Will an error occur when the member variable modified by final does not initialize it?
A: there will be errors. Because the java syntax states that final-modified member variables must be initialized by the programmer, the system does not implicitly initialize the variables.
As shown in the following figure, a compilation error occurs when a variable is not initialized:
Detail 3. Final modifies the difference between basic type variables and reference type variables.
If fianl modifies data of a basic data type, once assigned, it cannot be changed again.
So what about final embellishing reference data types? Can the referenced variable be changed?
Look at the following code:
Public class FinalTest {/ / assigns values private final static Student student = new Student (50, "Java") when declaring final instance member variables; public static void main (String [] args) {/ / a pair of final reference data types student changes student.age = 100; System.out.println (student.toString ());} static class Student {private int age Private String name; public Student (int age, String name) {this.age = age; this.name = name } @ Override public String toString () {return "Student {" + "age=" + age + ", name='" + name +'\'+'}';} / / below is the printed result Student {age=100, name='Java'}
As you can see from the print result, the age property that references the data type variable student can be modified successfully.
Conclusion:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
When final modifies a base data type variable, it cannot be reassigned, so the base data type variable cannot be changed.
For a reference type variable, it only holds a reference. Final only ensures that the address referenced by the reference type variable will not change, that is, it will always reference the object, but the properties in this object can be changed.
Detail 4. Final modifies the scene of local variables
The fianl local variable is initialized by the programmer and cannot be changed again if the final local variable is initialized.
If the final variable is not initialized, it can be assigned only once, and once it is assigned again, an error will occur.
The following code shows how final decorates a local variable:
Detail 5. Will final modification methods have an impact on overloading? What about rewriting?
For overloading: final can be overloaded after modifying the method
The code is as follows:
There is no problem with public class FinalTest {public final void test () {} / / overloaded method public final void test (String test) {}}
For overrides: when the method of the parent class is modified by final, the subclass cannot override the method of the parent class
As shown in the above code, you can see the compilation error prompt for cannot override and overridden method is final.
Details 6. The scene of final decorating class
When you modify a class with final, it indicates that the class cannot be inherited. In other words, if a class you will never let it be inherited, you can modify it with final.
The member variable in the final class can be set to final as needed, but note that all member methods in the final class are implicitly specified as final methods.
Detail 7. Write the reordering rules for final fields, do you know?
This rule prohibits write reordering of final fields outside the constructor, and the implementation of this rule consists of two main aspects:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
JMM prohibits the compiler from reordering writes in the final field outside the constructor.
The compiler inserts a StoreStore barrier after the final domain is written and before the constructor return. This barrier prevents the processor from reordering final domain writes outside the constructor.
For example, if it's not too abstract, let's take a look at a piece of code.
Public class FinalTest {private int a; / / normal domain private final int b; / / finaldomain private static FinalTest finalTest; public FinalTest () {a = 1; / / 1. Write the normal field b = 2; / / 2. Write the final field} public static void writer () {finalTest = new FinalTest ();} public static void reader () {FinalTest demo = finalTest; / / 3. Read object reference int a = demo.a; / / 4. Read the ordinary domain int b = demo.b; / / 5. Read final domain}}
Suppose thread An is executing the writer () method and thread B executes the reader () method.
Since there is no dependency between variable an and variable b, it is possible to reorder as shown in the following figure
Because the normal variable a may be reordered outside the constructor, it is possible for thread B to read the value (zero value) of the normal variable a before initialization, so an error may occur.
The final domain variable b, according to the reordering rule, forbids the final modified variable b to be reordered outside the constructor, so that b can be assigned correctly, and thread B can read the initialized value of the final domain variable b.
Conclusion: writing the reordering rule of the final domain ensures that the final domain of the object is initialized correctly before the object reference is visible by any thread, while the normal domain does not have this guarantee.
Detail 8: read the reordering rules for final fields, do you know?
This rule means that in a thread, the JMM forbids the reordering of the object reference and the final field contained in the object for the first time.
It's the same code above.
Public class FinalTest {private int a; / / normal domain private final int b; / / finaldomain private static FinalTest finalTest; public FinalTest () {a = 1; / / 1. Write the normal field b = 2; / / 2. Write the final field} public static void writer () {finalTest = new FinalTest ();} public static void reader () {FinalTest demo = finalTest; / / 3. Read object reference int a = demo.a; / / 4. Read the ordinary domain int b = demo.b; / / 5. Read final domain}}
Suppose thread An is executing the writer () method and thread B executes the reader () method.
Thread B may have the reorder shown in the following figure
As you can see, because the normal domain of the read object is reordered in front of the read object reference, thread B reads the ordinary domain variable of the object before reading the object reference, which is obviously the wrong operation. The read operation of the final domain "qualifies" that the reference to the object has been read before reading the final domain variable, thus avoiding this situation.
Conclusion: reading the reordering rules of the final domain ensures that you must read the references to the objects that contain the final domain before reading the final domain of an object.
At this point, I believe you have a deeper understanding of "what are the small details of the final keyword?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.