Is it good practice to use java.lang.String.intern()? -


the javadoc string.intern() doesn't give detail. (in nutshell: returns canonical representation of string, allowing interned strings compared using ==)

  • when use function in favor string.equals()?
  • are there side effects not mentioned in javadoc, i.e. more or less optimization jit compiler?
  • are there further uses of string.intern()?

when use function in favor string.equals()

when need speed since can compare strings reference (== faster equals)

are there side effects not mentioned in javadoc?

the primary disadvantage have remember make sure intern() of strings you're going compare. it's easy forget intern() strings , can confusingly incorrect results. also, everyone's sake, please sure document you're relying on strings being internalized.

the second disadvantage if decide internalize strings intern() method relatively expensive. has manage pool of unique strings fair bit of work (even if string has been internalized). so, careful in code design e.g., intern() appropriate strings on input don't have worry anymore.

(from jguru)

third disadvantage (java 7 or less only): interned strings live in permgen space, quite small; may run outofmemoryerror plenty of free heap space.

(from michael borgwardt)


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -