Why is the String class in Java decorated with final?

table of Contents

I. Introduction

Two, final keywords

Three, how to use the final keyword in the String class and its benefits

The source code of the String class

The benefits of immutability

Four, summary


I. Introduction

I believe that many friends have been asked during the interview, "Why is the String class in Java decorated with final?" Today, I will take you to find out. I hope that when you encounter this problem in the interview, you can The answer came out smoothly, and the high-paying offer was successfully obtained.

Two, final keywords

final is a reserved keyword in Java, which can be used to modify classes, methods and variables. Among them, the final modified class cannot be inherited, that is, it cannot have its own subclass, the final modified method cannot be overridden, and the final modified attributes and variables cannot be modified after initialization.

Three, how to use the final keyword in the String class and its benefits

The source code of the String class

From the above source code we can find the following two points:

  • The String class is decorated with final, which means that the String class cannot be inherited.
  • The char[] array used to store string values ​​is modified with private and final, where final can guarantee that the reference address of the value will not be modified, but it cannot guarantee that the value in the array will not be modified, and with the private modifier, it can The guaranteed value will not be externally modified. This will ensure the immutability of the String class.

So, what benefits can the immutability of the String class bring?

The benefits of immutability

The benefits of immutability of the String class are summarized in two main points:

  • Because of the immutability of the String class, the JVM can realize the string constant pool; the string constant pool can save a lot of memory space when the program is running, because when different string variables point to the same literal, they all point to the string constant The same object in the pool. This saves memory on the one hand, and improves performance on the other.
  • Because of the immutability of the String class, it is ensured that the string object is thread-safe in a multithreaded environment. If the String class is mutable, it will cause serious security problems. In many cases, we pass data directly through strings, such as database usernames and passwords, ip and port in network programming. Because the string is immutable, its value cannot be modified. If the string is available Change, then you can modify the value of the string by changing the value pointed to by the reference address, which leads to security vulnerabilities

Four, summary

 The String class is modified with final on the one hand to ensure thread safety in a multithreaded environment, on the other hand, it implements a string constant pool to save memory and improve performance.

                                                                Welcome to pay attention to Xiaoqiang 1024 Lab, and use technology to change the world with Xiaoqiang

Guess you like

Origin blog.csdn.net/u011147339/article/details/109231728