The use of the final keyword in the Android source code and the final,finally,finalize difference _android

Source: Internet
Author: User

Hi everyone, today, the weather drastic, very cold, do not want to go out, so I wrote an article on the android final keyword and final,finally,finalize of the difference between the relevant knowledge, details as follows:

First of all, the following is only a discussion of Java syntax, and the Android source is not a big deal, please do not read pressure.

I've found that in many parts of the Android source code, the final keyword usage is "ingenious," because I've never seen it using the final keyword. A typical example is the Onscrollchanged method in the view class (you might wish to make it a scenario):

  protected void onscrollchanged (int l, int t, int oldl, int Oldt) {
    mbackgroundsizechanged = true;
    Final Attachinfo ai = mattachinfo;
    if (AI!= null) {
      ai.mviewscrollchanged = true;
    }
  }

Did you see it? Here Mattachinfo is a member variable of the view class, and in this method the Android programmer does not directly manipulate the Mattachinfo variable, but first assigns the value to a local variable AI labeled Final and then operates the AI.

I can't figure out the wording of the writing, is it superfluous? But think about it and think it is not so simple, the battle of the Android development team to write this should not be groundless, is this writing really have other purposes?

Thought for a long time also guessed for a long time, a thought suddenly jumped out, is this writing because of the need for multithreaded programming? Consider the following wording (which may be considered as Programme II):

 protected void onscrollchanged (int l, int t, int oldl, int Oldt) {
    mbackgroundsizechanged = true;
 
    if (mattachinfo!= null) {//#1
      mattachinfo.mviewscrollchanged = true;//#2
    }
  }

In this writing, the final local variable AI is eliminated and the Mattachinfo is directly manipulated. Consider a scenario where, assuming that thread a executes the suddenly another thread B modifies the mattachinfo elsewhere and points it to another object, and when thread a executes to the snap, it will be the new object instead of the original object, and in scenario one, You can avoid this phenomenon.

The difference between the learning process of Android development final,finally,finalize

Android is growing faster and more and more Android developers, and when two situations collide, it is necessary to keep up with Android in many developers, although the Android development industry has high salaries and potential, but people go higher There is a greater room for ascent must seize the opportunity, the Android development industry is very simple, highly skilled industry industry's only important requirement is the skills, so learn Android development directly related to future career development, Thousand-front Android training institutions as the industry's most authoritative Android research and development and talent Training center, is the Education and Management Information Center certification of the National mobile Internet talent training base, professional teaching and improve the system to train a large number of highly skilled Android development talent, Thousand-Feng attaches importance to the foundation of students and follow-up development, from Java began the Devil-type high-intensity code training, Android application development and Android game development so that students have the strength to get a high salary, project actual combat theory knowledge system and project integration, increase the student's development experience.

 Android Initial phase learning in the process of Java training, Final,finally,finalize is different:

  Final: used to declare properties, methods, and classes. The modifier is the basic type, which means that the value given by the variable is immutable, that is, it is a constant; The final decorated method cannot be overridden by a quilt, but does not affect the inheritance of a subclass, and the final-decorated class cannot be inherited.

  finally: only in the try-catch-finally statement, whether there is an exception or no exception, this statement is always executed.

  Finalize: This method is a method in the object class (all classes have this method) and is part of the GC (garbage Collertor) run mechanism that is invoked when the GC cleans up objects it is subordinate to. If the execution of it has thrown an exception that cannot be caught, the GC terminates the cleanup of the object, and the exception is ignored, knowing that the next time the GC starts cleaning up the object, its finalize () is called again, and invoking their role is only recommended for GC startup, scavenging for unwanted objects to free up memory space, However, the start of the GC is not certain, which is determined by the Java virtual machine, knowing that the Java Virtual machine has stopped running.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.