0

Strings are immutable objects and are stored in the String Pool. Suppose in an application none of the strings are created using new operator. In this case also is it necessary to use equals method instead of == for String objects equality checks ? I feel the answer of above question is probably yes and it has something to do with String Pool size. How is the String Pool managed ? Memory is limited so I feel String pool also has a definite size. Does it work like LRU cache, discarding the least used Strings when the pool is full?

Please provide your valuable inputs.

My question is not about size of string pool. My question is if none of the strings are creared using new operator then using == will always be safe. Is this statement correct or can it happen that in this case also two string references haing same string characters may return false. I know design wise I should always use equals method butI just want to know the language specifications.

nits.kk
  • 5,204
  • 4
  • 33
  • 55
  • 3
    I believe that `equals()` should be used regardless of whether the strings be created usual `new` or not. – Tim Biegeleisen Mar 08 '16 at 02:30
  • Possible duplicate of [What if Size of String Pool Exceeds?](http://stackoverflow.com/questions/10224476/what-if-size-of-string-pool-exceeds) – Francisco Romero Mar 08 '16 at 02:30
  • 3
    I'm with @TimBiegeleisen. Sure there are some cases where you can use `==`, but I think it just says "Look how clever I am. I beat the system". And then one day it stops working and you don't look so clever anymore. Even if it does keep working. EVERYONE who reviews your code will pick it up and you'll have to explain - until you get sick of it and comment EVERY `stringOne == stringTwo` in your code to explain it works because you're so clever. Just do it the normal way and avoid all the issues! – John3136 Mar 08 '16 at 02:35

1 Answers1

3

Strings are immutable objects and are stored in the String Pool. Suppose in an application none of the strings are created using new operator. In this case also is it necessary to use equals method instead of == for String objects equality checks?

If you always use equals() you never need to worry about the answer to this question, but unless you only plan on comparing string literals the situation can never possibly arise.

I feel the answer of above question is probably yes

Correct.

and it has something to do with String Pool size.

No.

How is the String Pool managed? Memory is limited so I feel String pool also has a definite size.

No.

Does it work like LRU cache, discarding the least used Strings when the pool is full?

No, but Strings that have been intern()-ed can be garbage-collected from the pool.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • @EJB Thanks for the answer. I have edited my question, I know for objects equals should be used as == compares if the objects are same and does not check thw contents. I want some reference in relation to langauge specifications.. – nits.kk Mar 08 '16 at 05:39