Comparing String References

I am trying to understand java link comparison. Suppose we have the following main code:

public static void main (String args[]) {
   String str1 = "Love!";  
   String str2 = "Love!";
   String str3 = new String("Love!");  
   String str4 = new String("Love!");
   String str5 = "Lov"+ "e!";  
   String str6 = "Lo" + "ve!";  
   String s = "e!";
   String str7 = "Lov"+ s;  
   String str8 = "Lo" + "ve!";
   String str9 = str1;
}

I understand that str1 == str2 == str5 == str6 == str8 == str9all of them belong to the general pool. (meaning "Love!"). salso refers to a shared pool, but it refers to the value of "e!"

I also understand that str1 != s.

I know that str3, str4are links to HEAP, and each of them is a different object. str3 != str4.

I do not understand why , and I would like an explanation. str1 != str7

+4
source share
1 answer

AT

String s = "e!";
String str7 = "Lov"+ s;

"e!" , s (JLS ยง4.12.4); "Lov" + s, s, (JLS ยง15.28). s , final .

final String s = "e!";
String str7 = "Lov" + s;

str1 == str7 .

+13

Source: https://habr.com/ru/post/1682080/


All Articles