Using == when comparing objects

Recently in an interview I was asked the following question (for Java):

Given:

String s1 = "abc"; String s2 = "abc"; 

What is the return value?

 (s1 == s2) 

I replied that it would return false, because these are two different objects, and == is a comparison of memory addresses, not a comparison of values, and that you need to use .equals () to compare String objects. I was told that although .equals (0 methodology was correct, the statement nonetheless returns true. I was wondering if anyone could explain this to me why this is true, but why we are still learning how to use equals at school ()

+4
source share
7 answers

java will intern both lines, since they both have the same value, only one actual instance of the line will exist in memory - therefore == will return true - both links point to the same instance.

String interpretation is an optimization technique to minimize the number of string instances that should be stored in memory. String literals or strings that are constant expression values ​​are interned to exchange unique instances. Think flies .

+5
source

String constants are interned by your JVM (this is required by the specification here ):

All literals and string constant expressions are interned. String literals are defined in Β§3.10.5 Java Language Specification

This means that the compiler has already created an object representing the string "abc" , and sets both s1 and s2 to point to the same interned object.

+8
source

Since you are not actually creating new instances of String objects for one of them, they use the same memory space. If String s1 = new String("abc"); String s2 = new String("abc"); String s1 = new String("abc"); String s2 = new String("abc"); the result will be false.

+2
source

The reason is because strings are placed in Java. String interning is a method of storing only one copy of each individual string value, which is immutable. The inner string makes some string processing tasks more efficient. Different values ​​are stored in the pool of the pool. (from the wiki)

+2
source

You are correct that == uses a memory address. However, when the java compiler notices that you are using the same string literal multiple times in the same program, it will not create the same string multiple times in memory. Instead, s1 and s2 in your example will point to the same memory. This is called line breaking.

So why == will return true in this case. However, if you read s2 from a file or user input, the line will not be automatically interned. So now he no longer points to the same memory. Therefore, == now returns false, and equals returns true. And why you should not use == .

+1
source

Quick and dirty answer: Java optimizes strings, so if it encounters the same string twice, it will reuse the same object (which is safe because String is immutable).

However, there are no guarantees.

What usually happens is that it works for a long time, and then you get an unpleasant error that it finds forever, because someone changed the context of loading the class, and your == doesn't work anymore.

0
source

When testing string equality, you should continue to use equals (). Java provides no guarantee that strings are identical if they are not interned. The reason s1 == s2 in your example is because the compiler just optimizes 2 literals in the area that it can predict.

0
source

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


All Articles