This is my third article on getting Started

preface

First of all, let’s look at a familiar interview question. What is the following output?

public class Test {
    public static void main(String[] args) {
        String str1 = "hello";
        String str2 = "hello";
        String str3 = new String("hello");
        String str4 = new String("hello"); System.out.println(str1==str2); System.out.println(str1==str3); System.out.println(str3==str4); System.out.println(str1.equals(str2)); System.out.println(str1.equals(str3)); System.out.println(str3.equals(str4)); }}Copy the code

I’m sure all of you smart people know what the correct output is, and yes, it’s what you think it is, right

true
false
false
true
true
true
Copy the code

The reason for this is that equals compares the contents of two objectshelloSo it’s going to betrue.

Equals equals compares whether the addresses of the objects are the same, and we know that in the first wayString str1 = "hello"In this way, the compiler first checks to see if the string constant is in the constant pool. If it is not, it will be created. Then the JVM will create an implicit object pointing to the string constant in the stack space and create str1 in the stack space.

The second wayString str3 = new String("hello")In this way, we directly create an object in the heap memory, and create a “hello” string in the constant pool, this object points to the “Hello” string in the constant pool, and then create str3 variable, and the str3 variable points to the first address of the heap memory. So they’re actually distributed in memory like thisNow let’s talk about some of the features of the String class.

Immutable?

Open the source code for the String class, and you can see this sentenceIt is clearly stated here that the String class will not change once it is created, for the simple reason that the class is decorated by finalLet’s look at the next piece of code

public class Test {
    public static void main(String[] args) {
        String a1 = new String("Hello");
        String a2 = new String("World"); a1 += a2; System.out.println(a1); }}Copy the code

The output is HelloWorld, and so on. I just said that String is final and immutable. How can you just say that and then change? When this code executes, it first allocates a space in memory that is the sum of the size of A1 and A2, then copies the contents of A1 and A2 into that space, and finally points the variable a1 to that space, which is actually a new String. When we look at the methods in String, we can see that all the methods that change a String are actually returning a new object.

why

So why are strings designed to be immutable? The main reasons are as follows:

  1. The String type is the most used type in Java programs, which involves a lot of adding, deleting, and modifying. The JVM actually needs to check the safety of the String object before adding, deleting, and modifying it. And that’s the hashcode, when it’s designed to be immutable, that ensures that the hashcode is unique. Also can rest assured operation.
  2. Network connection address, URL, and file path are usually saved in String type. If String is not fixed, it will cause various security risks. Just as our password cannot be saved as a String, if you save it as a String in plain text, it will remain in memory until the garbage collector clears it. Because the string is placed in the string buffer pool for reuse, it can remain in memory for a long time, which can be a security hazard
  3. The string value is kept in the constant pool, which means that if the string object is allowed to change, there will be various logical errors.