Interview question: Why is string in Java set to final

Source: Internet
Author: User

1. Non-Change---execution efficiency is high

2. Because the object of string is basically used by all class objects, if it can be replicated, it will be very messy, such as the map key, if it is a string key, if the string can be changed, you think of the consequences

3. Implementation efficiency can be explained, object-oriented has a polymorphic nature, if it can be changed, can be replicated, sub-class if a method is replicated, the virtual function table is used, if the final type, the JVM directly to use, do not have to go to the virtual function table to find

----------as long as the declaration is final, the JVM does not need to query the related method in the virtual function table, but directly navigates to the related method of the string class, which improves the execution efficiency. base class to ensure that execution efficiency is the first element.

4. From the design idea to explain why this should be made final type

Language itself is a design. Any design idea is to abide by some established rules in order to embody consistency .     Both human language and machine language have their binding rules.  Long, Double, Integer and so on, all final. The cornerstone of the program is immutable 5. It is also possible to interpret the definition from a security standpoint as final,6.string is widely used, to avoid the effects of polymorphism, or the functions he provides are already very rich, and you can extend a little more.

Interview question: Why is string in Java set to final

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.