“This is the 28th day of my participation in the November Gwen Challenge. See details of the event: The Last Gwen Challenge 2021”.

The vast sea of millions, thank you for this second you see here. I hope my article is helpful to you!

May you keep your love and go to the mountains and seas in the coming days!

Note: As for the final keyword, it is also a keyword we often use. It can be modified on a class, or on a variable, or on a method to define some of its immutability!

The String class, for example, is final, and its character array is final. But have you really understood some details of the final?

Start with this article and take you deep into the details of final!

👋 Learn about final from the in-memory model

👩❤️👨 final objects are reference types

I’ve already seen that final field objects are a reordering rule for basic data types, but what about objects that are reference types? Let’s move on:

When a final field object is a reference type, the reordering rules for writing final fields add the following constraints:

There is no reordering between writes to the member field of a final referenced object within a constructor and subsequent assignments of references to the constructed object to reference variables outside the constructor. Sounds a little confusing, right? Ok, look at the code!

Note that the previous reorder rules for writing final fields still exist, except that one rule is added for reference type objects.

Code:

package com.nz.test;

/** * Tests read and write of final reference type objects */
public class ReferenceFinalTest {

    // Define a reference object
    final Person person;
    private ReferenceFinalTest referenceFinalTest;

    // Initialize in the constructor and perform the assignment
    public ReferenceFinalTest(a){
        person = new Person(); // 1. Initialize
        person.setName("James!); / / 2. Assignment
    }

    // thread A writes to initialize the referenceFinalTest
    public void write(a){
        referenceFinalTest = new ReferenceFinalTest(); // 3. Initialize the constructor
    }

    // Thread B comes in and writes the person.
    public void write2(a){
       person.setName("Davis"); // 4. Reassign
    }

    // Thread C comes in and reads the current person value
    public void read(a){
        if(referenceFinalTest ! =null) { // 5. Read the reference object
            String name = person.getName(); // 6. Read the person object's value}}}class Person{
    private String name;
    private int age;


    public void setName(String name) {
        this.name = name;
    }

    public String getName(a) {
        returnname; }}Copy the code

First, let’s draw a diagram of what might happen:

Our threads execute in order: A — > B — > C

Then we explain the read and write operation method in detail:

Write the final field reorder

As we already know, writes in our final field forbid reordering outside the constructor, so reordering is not allowed in 1 and 3.

For our new constraint, there is no reordering between writing to the member field of a final referenced object inside the constructor and then assigning a reference to the reference variable outside the constructor. That is, writing setName(” James “) to a member attribute of a reference object ina final field cannot be reordered with subsequent assignments of the constructed object to the reference variable jmmFinalTest, so 2 and 3 cannot be reordered.

So we go 1-2-3.

Read the final field reordering rules

In the case of multiple threads, the JMM memory model can at least ensure that thread C reads the reference object Person first when it reads the member properties of the object person, and that thread A can read the member properties of the final domain reference object Person.

Thread B may not be able to write the member attribute of Person for the time being, so the writing of thread B cannot be guaranteed to be visible to thread C, because thread B may compete with thread C in thread preemption, and the results may be different at this time!

Of course, if we want to preserve visibility, we can use Volatile or synchronous locking.

👨 ❤ ️ 👨 summary

We can classify them by data type:

Basic data types:

  1. Write: Pair inside the constructorfinalField writes, then assigns a reference to the constructor to a reference variable, and the operation cannot be reordered. banningfinalThe domain write is reordered outside the constructor.
  2. Read: To read an contain for the first timefinalDomain object reference and then first write thisfinalDomain, cannot be reordered.

Reference data types:

Add additional constraints on base data types:

Disallows writing a final-modified object’s member field property ina constructor and reordering the subsequent assignment of a reference to the constructed object to the reference variable.

🌸 summary

I believe that you will have a certain understanding and understanding of these small details of the final keyword. Believe me, after reading this, when the interviewer asks you the final word, it will definitely be amazed! After all, the fact that we’re so careful will definitely be a plus in this interview.

Here, we learn the final, in fact, not only for the interview, but also for us to accumulate experience, summarize knowledge and improve ourselves. We can never be satisfied with the details of a single point, right? We still have a handful of things to explore and explore! Next is to study for a period of time, not impetuous, not discouraged!

Let’s refuel together, too! I am not just, if there is any missing, wrong place, also welcome you to criticize in the comments of talent leaders! Of course, if this article is sure to help you a little, please kindly and lovely talent leaders to give a thumb-up, favorites, one key three even, thank you very much!

Here, the world is closed for today, good night! Although this article is over, I am still here, never finished. I will try to keep writing articles. The coming days are long, why fear the car yao ma slow!

Thank you all for seeing this! May you live up to your youth and have no regrets!