On the problem of String object in java

Why does the following program print false?
the book says that java uses the same instance of String objects with the same content
so why are references not equal?

public class test {
    public static void main(String[] args) {
        String s1="123";
        String s2=s1;
        String s3=new String("123");
        System.out.println(s3==s2);
        
    }

}
Mar.11,2021

to point to the same memory area, use the intern method

public class test {
    public static void main(String[] args) {
        String s1="123";
        String s2=s1;
        String s3=new String("123");
        System.out.println(s3==s2);
        System.out.println(s2==s3.intern());
        
    }

}

will output:

false
true

Java ensures that there is only one copy of a string constant , and when you build using new Sting (), new objects are always generated, not constants. The difference between a
constant and a variable is whether the value can be determined at compile time. A constant pool that exists in a .class file that is loaded by JVM at run time and can be expanded. String's intern () method is a method to extend the constant pool; when a String instance str calls the intern () method, Java looks up whether there is a string constant with the same Unicode in the constant pool, and if so, returns its reference, and if not, adds a string with Unicode equal to str in the constant pool and returns its reference.

you can refer to this link

http://www.runoob.com/java/ja.


the landlord can take a look at this article:
Java string (Part 2) related interview questions

after reading this article, you will understand


S3 is new, resulting in an object. S3 points to a NEW reference object address


There is a constant pool in

jvm. All created string constants are stored in it. New constants will be created if there are no constants in the constant pool.
new string () creates a new object with an area of memory. Regardless of whether the constant is in the constant pool or not.
the two values are the same, but the addresses are different. So false;

you can go to Baidu to String s3=new String ("123"); create several objects. Interview often asks


to be clear, as long as the new keyword, it is to create a new object,

String objects with the same content will use the same instance
The sentence

can be understood as the same reference in this case (the following code), which uses the constant pool technique to prepare the literal value in advance, and then use it directly when the literal value is assigned at that time.

String s1 = "abc";
String s2 = "abc";
System.out.println(s1 == s2);  // true

Let's start with why applications vary.
String S1 = "123"; / is to open up memory and create new objects, assuming that the variable s is a reference to a memory
String S2 = S1; / / the reference to a memory
String s3 = new String (" 123 "); / / re-apply for a section of memory, assuming that storage in area b
s3==s2 is to determine whether two variables point to the same block of memory. Obviously not, so print false

"the book says that java will use the same instance in String objects with the same content." in the case of String S1 = "123"; String S2 = "123"; S2 and S1 point to the same memory area.

Menu