0
public class Test2 {

  public static void main(String[] args) {

    String s1="Come back";
    String s2="Come back";
    String s3=s1;

    if(s1==s2)
        System.out.println("Equal");
    else
        System.out.println("Not Equal");

    if(s1==s3)
        System.out.println("Equal");
    else
        System.out.println("Not Equal");

}}

Output: equal equal, I expected not equal equal. My lecturer said that "==" statement compares references of variables.The references of s1 and s2 are different. Can anyone explain this?

Gary
  • 13,303
  • 18
  • 49
  • 71
Furkan
  • 117
  • 1
  • 2
  • 6

4 Answers4

3

Strings literals are interned, so literals with identical contents will actually use the same underlying reference.

You can intern any String, using intern():

String s1 = new String("a");
String s2 = new String("a");

// false
System.out.println("References equal? " + (s1 == s2));

s1 = s1.intern();
s2 = s2.intern();

// true
System.out.println("References equal after interning? " + (s1 == s2));
August
  • 12,410
  • 3
  • 35
  • 51
1
 String s1 = new String(“come back”); 

// create a new object in heap. Dynamic allocation, it is given by programmer

String s2 = “come back”;

// A new String object gets created only if a matching String object with the same value isn’t found in the String constant pool. Static allocation, memory is assigned by JVM

String s3 = "come back";

// using the string literal "come back" from the String constant pool

s1 == s2 // false
s2 == s3 // true
Bryan
  • 400
  • 1
  • 3
  • 15
0

In java, when you declare a new String it is added to the String pool if a string with the same value does not exist in the pool already. String pool allows JVM to reuse string constants that have already been added to the pool before to save memory. That's why when you define S2 with the same value as S1, the JVM is going to reuse S1 which already exist in the pool and therefore S1 == S2 returns true.

Denorm
  • 466
  • 4
  • 13
  • `when you declare a new String it is added to the String pool` this sounds like `new String("blub")` will add `"blub"` to the pool, but this is not the case. – Tom Dec 29 '14 at 01:59
  • That's true, it's ambiguous. By "declaring a new String" I meant when you do: String x = "blub". Thanks for the clarification. – Denorm Dec 29 '14 at 02:07
  • Maybe write something like *when you declare a String literal like `String test = "test"` it will be added to the String pool, if ....* – Tom Dec 29 '14 at 02:10
  • It's not ambiguous, it's just wrong. "Declaring" is when you create a variable by writing its type and its name. It's not when you use a String literal. So `String s1;` is declaring. `s1 = "Hello";` is using a literal and assigning a value. You should have written something like _When you use a `String` literal, it's added to the String pool if the same `String` is not already there._ – Dawood ibn Kareem Dec 29 '14 at 02:10
0

Use the String.equals(String other) function to compare strings, not the ==operator.

The function checks the actual contents of the string, the == operator checks whether the references to the objects are equal. Note that string constants are usually "interned" such that two constants with the same value can actually be compared with==, but it's better not to rely on that.

RishiKesh Pathak
  • 2,122
  • 1
  • 18
  • 24