Android source code final keyword usage and final finally finalize differences

  • 2020-11-18 06:27:03
  • OfStack

Hello everyone, today, the weather changes dramatically, it is very cold, I don't want to go out, so I write an article for you, about the key words of android final and the difference between final,finally and finalize, the details are as follows:

First 1, the following only involves java syntax discussion, and Android source is not relevant, please do not have reading pressure.

I find the use of the final keyword in many parts of the Android source code to be quite "inventive". I say this because I have never seen the final keyword used this way. A typical example is the onScrollChanged method in the View class (call it Scenario 1) :


  protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    mBackgroundSizeChanged = true;
    final AttachInfo ai = mAttachInfo;
    if (ai != null) {
      ai.mViewScrollChanged = true;
    }
  }

See? Here mAttachInfo is a member variable of the View class, and in this method Android's programmer does not manipulate the mAttachInfo variable directly, but assigns it to a local variable labeled final, and then to the ai.

I can't figure out how to write this. Isn't this one more thing? But if you think about it, it's not that simple. The experienced Android development team wrote it out of thin air. Is there any other purpose?

Think for a long time also guess for a long time, have an idea suddenly jumped out, is this kind of writing because of the need of multithreaded programming? Consider the following formulation (which might be called scenario 2) :


 protected void onScrollChanged(int l, int t, int oldl, int oldt) {
    mBackgroundSizeChanged = true;
 
    if (mAttachInfo != null) { // #1
      mAttachInfo.mViewScrollChanged = true; // #2
    }
  }

In this way, final's local variable ai is eliminated and mAttachInfo is manipulated. Consider such a scenario, suppose the thread A performed when performing # 2, # 1 will suddenly have another thread 1 B have modified to mAttachInfo elsewhere, it points to the other one object, then the thread A execution to # 2, the operation will be the new object rather than the original object, and in the scheme 1, you can avoid this kind of phenomenon.

Android developed the learning process final,finally,finalize difference

Android development faster and faster, Android more and more developers, collision, when two cases in many developers with tight Android pace to stand out is 10 points, although Android development industry salary is high, the potential is great, but man struggles upwards, have larger rising space 1 must seize the opportunity, Android development industry actually very simple, highly skilled industry industry important requirement is that only 1 skill, so learn Android development is directly related to future career development, Qian feng Android training institutions as the industry's most authoritative Android r&d and training center, is the education bureau of education management information center of the mobile Internet talent training base, professional teaching and perfecting the system of cultivating a large number of highly skilled Android development talent, qian feng attaches great importance to the students and the foundation of the follow-up development, starting from Java devil type high strength training, code Android application development and let students have got well-paid Android game development strength, the theory knowledge system and project in project practice, increase the students' development experience.

In the initial stage of Android training, there are differences among final,finally and finalize:

Final: Used to declare properties, methods, and classes. The value assigned to the variable is immutable, that is, it is a constant. final qualifies methods that cannot be overridden by subclasses without affecting their inheritance. final - decorated classes cannot be inherited.

Finally: Used only in the ES78en-ES79en-ES80en statement. The statement is always executed regardless of whether there is an exception or no exception.

Finalize: This method is a method of Object class (all classes have this method), is GC (garbage collertor) 1 part of the operating mechanism is in GC clean it is invoked when the object of the subordinate, if thrown in the execution of it into cannot capture anomalies, GC would end the clean up of the object, and the exception is ignored, know one GC began to clear the object, its Finalize () will be called again, call their purpose is to suggest GC start, Cleaning up unwanted objects frees up memory space, but the startup of GC is not fixed, it is up to the java virtual machine until the java virtual machine stops running.


Related articles: