-2

I am beginning to learn Java,so please pardon if the question seems silly, I was going through some online examples.

I found a String question, should not the output be true ?

As I am comparing two strings which are equal.

public class HelloWorld{

     public static void main(String []args)
     {
      String morning2 = new String("Morning");
      System.out.println("Morning" == morning2);
     }
}

Similarly for this program, should not this print True

public class HelloWorld{

     public static void main(String []args)
     {
        String str1 = new String("Paul");
        String str2 = new String("Paul");
        System.out.println(str1 == str2);
     }
}

But for this program, it prints true.

public class HelloWorld{

     public static void main(String []args)
     {
        String str3 = "Harry";
        String str4 = "Harry";
        System.out.println(str3 == str4);
     }
}

Can anyone explain why is there difference in output ?

I have seen this question and answer How do I compare strings in Java? but It does not really fit the explanation.

Community
  • 1
  • 1
Sleez
  • 107
  • 1
  • 10
  • `Object` including `String` are always compare using `equals`, not `==` –  Oct 03 '15 at 08:39
  • Java only has primitive and references. When you use `==` you are comparing references. In the first two examples, you have different objects so `==` returns `false`, in the last example, you have the same object through string literal pooling. Thus you have the same object twice and `==` returns `true`. – Peter Lawrey Oct 03 '15 at 09:31

3 Answers3

1

The reason why your 1st program

public class HelloWorld{

     public static void main(String []args)
     {
      String morning2 = new String("Morning");
      System.out.println("Morning" == morning2);
     }
}

prints false is because in these lines

String morning2 = new String("Morning");
System.out.println("Morning" == morning2);

you are creating different object(by new keyword) and comparing with a string, eventhough the strings are same, you are here comparing two different objects.

Thats how java allocates Strings in the String pool.

Similary for the second program.

But for the 3rd program, what is happening is, when you execute line

String str3 = "Harry";

The string object Harry is allocated in string pool, when you execute the next line,

String str4 = "Harry";

Java searches if this String objects is available in the String pool, since it is available it does not create new String Object, and it references from this object itself.

So you get true in 3rd program.

Hope I have cleared your query.

penta
  • 2,536
  • 4
  • 25
  • 50
0

You must use "Morning".equals(morning2) to check the String Objects.

take a look at this post: How do I compare strings in Java?

Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

This method compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.

public class HelloWorld{

     public static void main(String []args)
     {
        String str1 = new String("Paul");
        String str2 = new String("Paul");
        System.out.println(str1.equals(str2));
    }
}
ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42