1. Why is the string class final?

Source: Internet
Author: User

Best Answer: mainly for the sake of "efficiency" and "safety" . If string is allowed to be inherited, the performance of the program may be reduced due to its high frequency use, so string is defined as final.

string is the object type. Because the object type must call a static method or value under a static method, it must be instantiated under a non-static method. The main function is modified by static. The string is defined as final, which is also:string is the object type, no instantiation under the main function, and can be called directly as other basic types without reporting the cause of the error.

when the string type is modified by the static and final adornments, the program speeds up. This, too, is true for the original data type.

For example: private static final String x = "Example";
For this static constant (identified by the final keyword), a temporary string object is created each time a constant is used, and in the bytecode, the compiler replaces the string "example" with "X", and the JVM makes a hash table query each time the "x" is referenced. In contrast, for a static variable (not the final keyword), the string is created only once. The VM does a hash table query only when the "X" is initialized.

classes with the final modifier are not derived. In the Java Core API, there are many examples of final applications, such as: Java.lang.String, which specifies final for the string class, which prevents the programmer from overwriting the length () method.

If you specify a class as 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%.

Example:

If string is not final then it can be inherited

public class String2 extends String{
// ..
}
Then our main can be written as well.

public class Test {
public static void Main (string2[] args) {//Note here
//
}
}

Add one point: The final class cannot be inherited and cannot be inherited by other classes, meaning: security.

Java from the day of birth is "serving the people", which is why Java can not do virus, it does not necessarily have to be a virus, anyway is for the sake of security, others Java developers are not to let Java do this kind of dangerous things, Java is not the operating system local language, In other words, Java must rely on the power of the operating system itself to do things, the JDK provides a lot of core classes such as String, such a class of the internal implementation of a lot of methods are not written by the Java programming language itself, a lot of methods are called operating system local API, this is the famous " Local method Call ", this is the only way to do things, this class is very low, and the operating system to communicate frequently, if this class can be inherited, if we re-rewrite its methods, to the operating system to write a malicious code of the nature of what, this is not a core virus it?

  So do not want others to change, this class is like a tool, the class of the provider to provide us with, we hope that we can directly use the end, do not want us to change, in fact, frankly or security, if you can change, then Java program is certainly very unstable, you can ensure that they do not mess, But in the future a lot of people to do, can not control others, and sometimes in case of negligence? He's not an estimate, so this security is important, and one of the advantages of Java versus C + + is this.

1. Why is the string class 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.