Why is a string in Java final

Source: Internet
Author: User

Final concept:

If a class is declared final, it means that it can no longer derive a new subclass and cannot be inherited as a parent. Therefore, a class cannot be declared both abstract and final.

Declaring variables or methods as final ensures that they are not changed in use. Its initialization can be in two places:

The first is its definition, that is, to assign a value directly to the final variable definition ;

The second is in the constructor .

These two places can only be selected, either in the definition of the value, or in the constructor to give a value, not both in the definition of the value, but also in the constructor to give additional values, and in subsequent references can only read, not modifiable. A method that is declared final can also be used only and cannot be overridden.

  the final variable, which represents the variable as a constant , and must be assigned a value .

Static Final int i = 123;

Or

Static Final int i; Static  = 123;}

Why is string a final class?

Through the above narrative, can say:

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 usage rate, so string is defined as final.

Java is not the operating system's native 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 classes of the internal implementation of many methods are not written by the Java programming language itself, Many methods are called the operating system local API, this is the famous " local method call ", but only in order to do things, this class is very low-level, and the operating system to communicate frequently, then if this class can be inherited, if we re-rewrite its methods, Is it a core virus to write a malicious code inside the operating system?

Why is a string in Java 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.