Why is string in Java designed to be final

Source: Internet
Author: User

a variety of different understandings1The main purpose of declaring a method or class as final is to make sure that they do not change the semantics in subclasses. The string class is the final class, which means that no one is allowed to define a subclass of string. In other words, if you have a reference to a string, it must refer to a string object, and it cannot be an object of another class.
--"Java Core technology Volume I"2For example, a method might have accepted a string type and returned its uppercase style
public static String uppperString(String s){ return s.toUpperCase();}

When you pass in string s= "test", he does not modify the string pool "test", but instead directly creates an instance of "Test" to return. But if your stringchild touppercase () is overridden by you (override) for mutable, and then you call this method when you pass in the Stringchild instance, then the whole (depends on (over) Method uppperstring the behavior of all classes) is likely to be disordered.

To be aware, string is a class that is used by almost every class, especially when the key value of a collection, such as HashMap, has a very high risk of mutable string. And once it happens, it's very hard to find. Declares that the string is final once and for all.
3String Objects isImmutable, which means that once created, their values cannot be changed. The String class is not technically a primitive data type, but considering the special support given to it by the language , you'll probably tend to think of it as such.4String is final because of the object-oriented language characteristics: The polymorphism of the class, and the use of a string by super-wide.

Assume that a string is not a final decoration, but that it can be inherited. Design a class mystring inherit String, rewrite length (), and return 0.

Where a string object is needed, the MyString object is used, because the polymorphism of the class does not result in any compilation errors, but the consequences ....
5Unlike other basic types, string is an object type . Since it is an object type, if static methods or values must be called under static methods, it must be instantiated if it is a non-static method .
The main function is static . So a string can be called directly like any other base type. This is why the string type does not report an error under the main function :
Explain the two hearts of the questions in a moment .
It used to be strange why string is an object type that does not need to be instantiated under the main function . Again, I admire Java designers for their thoughtful thinking .
6
When you defineA static field (also a class field) of type string that can be used as a static variable (non-Final) instead of constant (Final) speed up the program. Conversely, for raw data types, such asint, also established.

For example, you might create one of the followingString object:

private static final String x = "Example";

For this static constant (by theThe final keyword is identified), and each time you use a constant, you will create a temporaryA string object. In the byte code, the compiler removes"X", instead of it is a string"Example" so that each reference"X" the VM will make a hash table query once.

In contrast, a static variable (not the final keyword ), the string is created only once. theVM does a hash table query only when the "X" is initialized.

There is another explanation :
A class with a final modifier is not derived. In the Java core API, there are many examples of final applications, such as java.lang.String. specifying final for the string class prevents people from overwriting the length () method.

In addition, if you specify a class to be final, all methods of that class are final. The Java compiler looks for the opportunity inline (inline) for all final methods (this is related to the specific compiler implementation). This will increase the performance by an average of 50%.







Why is string in Java designed to be final

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.