If == compares links in Java, why does it evaluate true with these lines?

As indicated, the == operator compares object references to see if they refer to the same object on the heap. If so, why am I getting "Equal" for this piece of code?

public class Salmon { public static void main(String[] args) { String str1 = "Str1"; String str2 = "Str1"; if (str1 == str2) { System.out.println("Equal"); } else { System.out.println("Not equal"); } } } 
+13
java string reference
Oct 27 2018-10-27
source share
5 answers

The program will print Equal . (at least using the Sun access point and the Javac sun.) This is shown here at http://ideone.com/8UrRrk

This is because the string literal constants are stored in the string pool, and string references can be reused.

Further reading:




This however:

 public class Salmon { public static void main(String[] args) { String str1 = "Str1"; String str2 = new String("Str1"); if (str1 == str2) { System.out.println("Equal"); } else { System.out.println("Not equal"); } } } 

Not equal will be printed, since new guaranteed to present a new link.

So, a rule of thumb: Always compare strings using the equals method.

+37
Oct 27 '10 at 13:20
source share

This code will not print Equal .
But if the two lines were the same, this case would be special.

Now that you have updated your code, it is like this:

A simple (but not entirely accurate) explanation is that the compiler sees that the two lines are the same and do something like:

 String str1 = "Str1"; String str2 = str1; 



What actually happens here is that the compiler will see the literal string and put it in the "Linear String Pool".

Since the line cannot be changed (it is unchanged), the literal values ​​of the lines (found at compile time) are placed in the "pool".
Thus, if two different literal strings having the same content (for example, in this particular case), the memory is not wasted to store "Str1" and "Str1" twice.

+3
Oct 27 2018-10-27
source share

Java stores all the rows in the row table inside during the run. References to two lines are identical, since they are stored in one place in memory. Therefore, Equal .

Your statement is true that == compares object references. Try to do the same with any class other than strings and you will not get the same result.

+3
Oct 27 2018-10-27
source share

People, you forget that the process of placing literal strings in a pool is called "interning." The String class has an intern () method. This method places any row in the pool, even if it is not in the pool initially (not literally). This means that the code is:

 String a = "hello"; String b = new String("hello"); b = b.intern(); System.out.println(a == b); 

prints " true ". Now why does anyone need this? As you can imagine, comparing a.equals(b) strings can take a long time if the strings are the same length but close to the end. (Look at the source code for .equals ().).

However, link comparison directly matches integer matching (pointers in C say), which is close to the moment.

So what does this give you? Speed. If you have to compare the same lines many times, the performance of your program will be very useful if you put these lines. If, however, you only compare strings once, then there will be no increase in performance since the interning process itself uses equals ().

Hope this explains it.

thank

+2
Oct 27 2018-10-27
source share

The comments above summarized it pretty well.

I don’t have a Java environment, but trying to do the following should clarify everything for you (I hope this works as I expect).

 String str1 = "Str1"; String str2 = "Str"; str2 += "1"; 

Now you need to print Unevenly

0
Oct 27 '10 at 13:50
source share



All Articles