The benefits of a Java local variable plus final modification

Source: Internet
Author: User
In general, there are several uses for the following:
1, for the loop, the use of local variables to save the loop several times, and final modification, rather than directly with GetCount (), GetSize (), lenght, etc.
2, when you need to access an object in the collection, use local variables to refer to, and final decoration, rather than direct reference
3, need to access an external object, using local variables to reference, and final decoration, rather than direct reference
4. Other circumstances

The benefits of personal understanding are:
1. Accessing local variables is faster than accessing member variables
2, access to local variables than each call method to get objects faster
3. Use final decoration to avoid variable assignment (reference assignment)
4. When using final adornments, the JVM does not have to keep track of whether the reference has been changed.

Here are some insights to see on the Web:

Accessing a local variable is faster than accessing a field.  It ' s best to keep field accesses out of performance-critical inner loops when possible.  (Profile a course, to the if it matters.) In theory the JVM could "inline" the field to a local variable automatically under the right conditions, but don ' t count O n it.

A final local variable isn't any different the from the normal local variable at runtime. The "final" keyword on a local variable expresses a constraint on the source code (which is assigned to once) which the Compiler can easily check.

Final *fields* do allow additional optimizations, and static final fields with primitive values (as in) public static FINA l INT my_constant = 3 ") are treated as Compile-time constants and inlined.
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.