0

Possible Duplicate:
Java String.equals versus ==

Why when we declare string in Java we can't use == to compare this string and it will always turn to false, but if we initialize the string from the beginning it will be true?

For example :

import java.util.Scanner;

public class MyString {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String s = input.nextLine();
    if(s=="Hello")
        System.out.println("Hello");

    String d = "Hello";
    if(d=="Hello")
        System.out.println("Hello");
}

}

What is the explanation for this behavior?

Community
  • 1
  • 1
user1816808
  • 13
  • 1
  • 2

5 Answers5

3

This is an example of String.intern() happening automatically for string literals but not in general.

If you change your code to

public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner input = new Scanner(System.in);
    String s = input.nextLine();
    s = s.intern();
    if(s=="Hello")
        System.out.println("Hello");

    String d = "Hello";
    if(d=="Hello")
        System.out.println("Hello");
}

you will see "Hello" printed twice upon entering "Hello" at the console, because then all the copies of the "Hello" will have been interned to the same copy.

You should of course not normally use == to compare Strings, but use

if (s.equals("Hello")

This "intern" process is a way of reducing memory usage supported by many languages including Java. When you call s.intern() the run-times looks for a copy of the string in a pool of interned strings, uses one if it's found, and makes one otherwise, so that there's only one copy of that string. For more on the general idea, see this Wikipedia article.

Don Roby
  • 40,677
  • 6
  • 91
  • 113
2

Use str.equals(str2). Otherwise you are comparing whether the objects have the same address.

djechlin
  • 59,258
  • 35
  • 162
  • 290
2

The reason is that the string object returned by input.nextLine() is not interned. So, it's not the same string object as the one represented by the string literal "Hello".

With the following, if you enter "Hello", you should see the difference:

Scanner input = new Scanner(System.in);

String s = input.nextLine();
s = s.intern();

if (s == "Hello") {
    System.out.println("Hello 1");
}

String d = "Hello";

if (d == "Hello") {
    System.out.println("Hello 2");
}
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
1

use .equals() method to check string equality. == checks if two reference variables point to the same string object.

Marvo
  • 17,845
  • 8
  • 50
  • 74
PermGenError
  • 45,977
  • 8
  • 87
  • 106
  • While good advice, this doesn't really answer the question as to why the second compare works differently. – Don Roby Nov 11 '12 at 23:25
-1

nextLine() adds a "\n" to the end of the entered string, rendering it unequal to "Hello".

apparatix
  • 1,492
  • 7
  • 22
  • 37
  • That's not right: http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html#nextLine%28%29 – Bhesh Gurung Nov 11 '12 at 23:04
  • "The position is set to the beginning of the next line." – maybe you're correct. I'm just going off my Computer Science class knowledge ;) – apparatix Nov 11 '12 at 23:10
  • Welcome to Computer Science. =) Even if the scanner did append a \n to the string, the issue is one of comparing reference vs. comparing instances of objects. The == operator compares the *references* to objects. The equals() method compares the equality of the instances themselves. – Marvo Nov 11 '12 at 23:19
  • Agreed, others are definitely correct. – apparatix Nov 11 '12 at 23:30