-1

System.out.print("a".replace('a','1')=="a".replace('a','1'));

true was expected as replace() returns String and String comparison is possible with ==. But the above code prints false. Explanations?

Jean-Bernard Pellerin
  • 12,556
  • 10
  • 57
  • 79
CᴴᴀZ
  • 521
  • 7
  • 20
  • 1
    You shouldn't compare strings in java with `==`. It checks if their references are the same, not their values. – Corey Ogburn May 03 '13 at 20:39
  • I understood the concept of **String Interning** via the above post by Sir Nizet. That's the reason for `S.O.Pln ("a"=="a");` returning `true`. Thank you all for your time and co-operation. :) – CᴴᴀZ May 03 '13 at 20:50

2 Answers2

4

== checks if they are the same object in memory, so at the same location.

Since these are two distinct strings that just happen to have the same content you need to compare with .equals which compares values not reference.

Jean-Bernard Pellerin
  • 12,556
  • 10
  • 57
  • 79
  • Or if you want to compare the number values you are replacing them with, you need to parseInt that new replacement and THEN you can use `==` – aug May 03 '13 at 20:41
  • Sir Jean:S.O.Pln ("ab"=="ab"); returns `true`. Do they share same memory? – CᴴᴀZ May 03 '13 at 20:44
  • 1
    @ChaZ In this case yes because they are known at compile time so they are given a static location in memory and not created at run-time. It's a tricky case. To be safe, always compare strings with .equals unless you specifically know you care about memory location. – Jean-Bernard Pellerin May 03 '13 at 20:45
  • @Jean-BernardPellerin:Will do. Btw, it's just an academic question. I very well knew the use of `equals()` but now, well understand the risks of `==` too. Well appreciated! :) – CᴴᴀZ May 03 '13 at 20:58
1

No, string comparison is not advisable with ==. In certain cases e.g.:

new String("test") == new String("test")

it returns false.

All this is because theoretically Java should search for certain string in string pool, but in practice there are operations that returns new object instead of the one that resides in string pool.

This is very important to always use "equals" method instead of ==!!

Michal Borek
  • 4,584
  • 2
  • 30
  • 40
  • 1
    I would go so far as to say in *most* cases the equality test will return false. – stevevls May 03 '13 at 20:40
  • 4
    I would go so far as to say never use `==` to compare strings. – Corey Ogburn May 03 '13 at 20:41
  • Agreed. Was an academic question, that's the reason I asked. Btw why `S.O.Pln ("ab"=="ab");` returns `true`? They don't share same memory I suppose. – CᴴᴀZ May 03 '13 at 20:45
  • 1
    They do, read about string pool. – Michal Borek May 03 '13 at 20:47
  • @MichalBorek:Thanks got the idea. For fairly utilizing the memory and proper storage of duplicate Strings. :) – CᴴᴀZ May 03 '13 at 20:51
  • 1
    This knowledge is reeeaally important. You may have memory problems if some stupid method creates a lot of same String objects in memory. You can then use "intern()" method to force JVM putting it in string pool to free memory. Study this topic because from my experience it may help you find some performance problems in future :) – Michal Borek May 03 '13 at 20:54
  • @MichalBorek:Sure Sir Borek, I hope this will help me in my project. Great pleasure to receive a help from ur side. :) – CᴴᴀZ May 03 '13 at 20:56