Interview must be asked: Why is the final type of String

Meaning the final keyword

final is a reserved keyword in Java, can declare member variables, methods, classes, and local variables. Once you declare the reference as final, you will not be able to change the references, the compiler will check the code, if you try to re-initialize a variable, then the compiler will report compilation errors.

Benefits final keyword

  • final keyword improved performance. JVM and Java applications are cached final variable.
  • final variables can safely be shared in a multithreaded environment, without the need for additional synchronization overhead.
  • The final keyword, JVM will method, class variables and optimization.

Important points on the final

  • The final keyword can be used for member variables, local variables, methods and classes.
  • final member variables must be initialized when they are declared or initialized in the constructor, otherwise it will report compilation errors.
  • You can not assign a variable final again.
  • Local variables must be assigned at the time of declaration.
  • In the anonymous class in all variables must be final variable.
  • final methods can not be overridden.
  • final class can not be inherited.
  • Unlike the final keyword finally keyword, which is used for exception handling.
  • readily final keyword () method confused with finalize, the latter method is defined in the Object class, before the garbage collection method is called JVM.
  • All variables declared in the interface itself is final.
  • final and abstract both keywords are inversely related, final class can not be abstract of.
  • final method at compile time binding, static binding (static binding).
  • Uninitialized variable called final blank final variable (blank final variable) in the statement, they must be initialized in the constructor, or by calling this () to initialize. Do not do so, the compiler will complain "final variable (variable name) needs to be initialized."
  • Classes, methods, variables declared as final can improve performance, so have the opportunity to estimate the JVM, then optimize.
  • According to the Java code convention, final variable is constant, and usually constant names should be capitalized.

String class benefits of immutability

  • Only when the strings are immutable, the string pool be possible to achieve. Achieve string pool can save a lot of heap space at run time, because the same string different string variables are pointing to the pool.
  • If the string is variable, it can cause serious security problems. Because strings are immutable, so its value can not be changed, otherwise change the value of the object pointed to a string, resulting in vulnerabilities.
  • Because strings are immutable, it is multi-thread safe, the same instance of a string can be shared by multiple threads. This will not be used because of security thread synchronization. String own is thread-safe.
  • Class loader to use strings, immutable security is provided, so that the correct classes are loaded. For example, you want to load java.sql.Connection class, and this value is changed myhacked.Connection, it will cause damage to your database agnostic.
  • Because strings are immutable, so when it was created hashcode cached, you do not need to be recalculated. This makes it suitable as a string of keys in the Map, the processing speed of the string to be faster than the other key objects. This is often the key in HashMap string.

Welcome to sweep the code number of public attention, better communication

Published 115 original articles · won praise 67 · Views 100,000 +

Guess you like

Origin blog.csdn.net/meifannao789456/article/details/100079527