The String class in the JDK was designed as the reason for the final

  • 2020-04-01 01:22:46
  • OfStack

The best answer :
Mainly for the sake of "efficiency" and "safety". If String is allowed to be inherited, it may degrade the performance of the program due to its high utilization, so String is defined as final.
Other answer one :
Unlike other primitive types, String is an object type. Since it is an object type, you must call static methods or values if you are in static methods, and you must instantiate non-static methods.
The main function is static, so String should be called directly like other basic types.
This explains the two questions in my mind...
I've always wondered why String is an object type that doesn't need to be instantiated under the main function.
Other answer two :
When you define static fields of type String (also as class fields), you can use static variables (non-final) instead of constants (final) to speed up your program. Conversely, for primitive data types, such as int, this is also true.
For example, you might create a String object like this:
 
private static final String x = "example"; 

For this static constant (identified by the final keyword), you create a temporary String object every time you use the constant. In byte code, the compiler removes the "x" and replaces it with the string "example" so that the VM makes a hash table query every time "x" is referenced.
In contrast, for static variables (non-final keywords), strings are created only once. The VM queries the hash table only when the "x" is initialized.
There is another explanation :
Classes with final modifiers are not derivable. 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 a class is specified as final, all methods of that class are final. The Java compiler looks for opportunities to inline all final methods (depending on the specific compiler implementation). This can improve performance by an average of 50%.
Example:
 
public class Test { 
public static void main(String[] args) { 
// 
} 
} 

If String is not final then you can inherit
 
public class String2 extends String{ 
//.. 
//... 
} 

So we can also write our main
 
public class Test { 
public static void main(String2[] args) { //Note here
// 
} 
} 

One more thing :
The effect is that final classes cannot be inherited, so what's the advantage of not being able to let others inherit?
The point is, security, and so on:
Java are born is to "serve the people", which is why Java virus can't do it, also don't have to be virus, anyway, all in all is for the sake of safety, the somebody else Java developers is to don't want to let Java do such dangerous thing, Java is not the local language, operating system, in other words the Java must use the power of the operating system itself can do things, in the JDK to provide a lot of core classes such as String, inside a lot of the implementation of the methods of this kind of classes are not written in the Java programming language itself, Many methods are all calls to the operating system of the local API, this is the famous "local method calls", also is the only way to do things, this class is very bottom, and the operating system exchange frequently, so if this class can be inherited, if we put it rewrite, to write a operating system internal characteristics to malicious attack code of what, it's not become the core virus?
Described above is one of the most important, another aspect, the above two man said is very right, just don't want others to change, this kind of like a tool, class provider offers us, hope we directly use finished, don't want to let we can casually change, in fact, to put it bluntly or security, if any can be changed, so the Java program is very unstable, certainly you can keep yourself not disorderly change, but a lot of people to do a project in the future, tube is not others, and sometimes neglect one thousand? It's also not estimable, so this security is important, and one of the advantages of Java over C++ is this;
The reason is absolutely not only so many, because if these core classes can be arbitrarily manipulated, it is very terrible, there will be a lot of unknown errors, inexplicable errors... .

Related articles: