0

I was doing some java tests to practice and I came across a question that I don't understand. I created a small program to test it:

The question was to say what would be the output of System.out.println(ab==abc);

I answered 'true' thinking that String literals are not objects, so the can be seen as a kind of primitive type so the comparation == would compare the values and nothing to do with references. But actually the answer in "false";

Then I did this test and I even print the outputs and as you can see ab and abc are exactly the same, however the comparation is returning false , but if I do the comparation directly without doing any concatenation (as I did at the end of the program) the comparation is returning true. So it seems clear the reason has to be with the concatenation, I know that Strings are inmutable so when concatenating then we are getting another String literal with exactly same value.

Can someone please explain me who't going on here?

For those telling me that String literals are objects, why then this code returns true?

  String p="meowdeal";
  String o="meowdeal";

   System.out.println(o == p);
   //output true

Of course I would understand that this code String o=new String("meowdeal"); String p=new String("meowdeal"); System.out.println(o==p);

returns false because in that case they are really objects but not when they are String literal, am I right?

Thank you for your time

  public static void main(String ads[] ){


    String a="meow";
    String ab=a+"deal";
    String abc="meowdeal";


    System.out.println(a);
    System.out.println(ab);
    System.out.println(abc);
    System.out.println(ab == abc);

    //output
    //meow
    //meowdeal
    //meowdeal
    //false



    String p="meowdeal";
    String o="meowdeal";

    System.out.println(o == p);
    //output
    //true

}
fgonzalez
  • 3,787
  • 7
  • 45
  • 79
  • http://stackoverflow.com/questions/767372/java-string-equals-versus – michaelgulak Mar 08 '15 at 15:25
  • `String` is not a primitive type, and a string literal is represented by a `String` object. so `==` does reference equality on `String` variables, just like with any other variable of non-primitive type. – Jesper Mar 08 '15 at 15:27

3 Answers3

1

ab and abc are objects so .equals() is used to see if they have the same contents and == is used to see if they are the same object.

The last test is only true due to a compile optimization known as string interning

(Your second comment below is correct)

user1133275
  • 2,642
  • 27
  • 31
  • So if String literals are objects why String p="meowdeal"; String o="meowdeal"; System.out.println(o == p); returns true? – fgonzalez Mar 08 '15 at 15:32
  • @fgonzalez Just updated answer to that – user1133275 Mar 08 '15 at 15:36
  • So then, If I understood well, in the first case the compiler was not doing this pooling of Strings because I was concatenating two Strings, so then the compiler did not realized that they were the same at the end, then System.out.println(ab == abc); was false. But in the second case both of them was declared directly with the same value so compiler optimised and it only created a reference to the same value. Am i right? – fgonzalez Mar 08 '15 at 16:09
0

String literals are Objects. User equals method instead of == operator.

Just to explain what happen here,

String p="meowdeal"; //creates "meowdeal" in heap
String o="meowdeal"; // o also refer to same object in heap.

This is called pooling of String. Since both refer to same memory location == operator returns true.

Don Srinath
  • 1,565
  • 1
  • 21
  • 32
0

In Java you should compare Strings with .equals(String s) because the '==' returns only true if it's the exact same String in the memory and doesn't compare if the content is the same. In your first example you've got two different strings in your memory. In the second, java saw, that these would be the same and allocated only space for one string and your p and o are only a reference to the same object.

neofu
  • 108
  • 7