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