Best Answer:
It is mainly for the sake of "efficiency" and "security. If the String is allowed to be inherited, its height is used, which may reduce the program performance, so the String is defined as final.
Other answer 1:
String is an object type different from other basic types. since it is an object type, if it is a static method that must call a static method or value, if it is a non-static method, it must be instantiated.
The main function is static, so the String can be called directly like other basic types. This is why the String type in the main function does not report errors ..
I explained two questions ..
I used to wonder why the String type does not need to be instantiated under the main function. I once again admire the thoughtful thinking of java designers.
Other answer 2:
When defining static fields of the String type (also class fields), you can use static variables (non-final) instead of constants (final) to speed up the program. Otherwise, the original data type, such as int, is also true.
For example, you may create the following String object:
Copy codeThe Code is as follows: private static final String x = "example ";
For this static constant (identified by the final keyword), you will create a temporary String object every time you use the constant. In byte code, the compiler removes "x" instead of the string "example", so that the VM performs a Hash Table query every time "x" is referenced.
In contrast, a string is created only once when it is a static variable (not a final keyword. The VM performs Hash Table query only when "x" is initialized.
There is another explanation.:
Classes with final modifiers cannot be derived. There are many examples of final applications in the java core API, 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 as final, all the methods of this class are final. The java compiler will look for opportunities to inline (inline) All final methods (this is related to the specific compiler implementation ). This can increase the performance by an average of 50%.
Example:Copy codeThe Code is as follows: public class Test {
Public static void main (String [] args ){
//
}
}
If String is not final, it can be inherited.Copy codeThe Code is as follows: public class String2 extends String {
//..
//...
}
Then our main can be writtenCopy codeThe Code is as follows: public class Test {
Public static void main (String2 [] args) {// note this
//
}
}
Additionally:
The role is that final classes cannot be inherited. What are the benefits of failing to inherit from others?
The significance lies in the security, so that:
Java was "serving the people" since its birth. That is why java cannot do viruses, and it doesn't have to be viruses, java developers do not want java to do such dangerous tasks. java is not a local language of the operating system. In other words, java must use the power of the operating system to do things, many of the core classes provided by JDK, such as String, are not implemented by the java programming language. Many methods are called by the local API of the operating system, this is the famous "local method call". Only in this way can we do things. This type is very low-level and frequently communicates with the operating system. If this type can be inherited, if we rewrite the method and write a piece of malicious attack code into the operating system, will this become the core virus?
The above is the most important thing. On the other hand, the two old guys mentioned above are also right, that is, they do not want to be changed. This class is like a tool, the class provider provides us with a solution. We hope we can use it directly and don't want us to modify it at will. To put it bluntly, we still have security. If we can change it at will, so the program written in java is definitely unstable. You can ensure that you do not modify it. But in the future, many people will do it in a project, so you can't manage others. Besides, what if you neglect it sometimes? This is not an estimate, so this security is very important. This is one of the advantages of java compared with C ++;
There are definitely not so many reasons, because if these core classes can be operated at will, it is terrible and there will be many unknown and inexplicable errors ....