Comments on: String.intern() – there are better ways https://grey-panther.net/2010/03/string-intern-there-are-better-ways.html Just another WordPress site Sat, 01 Jan 2011 15:24:59 +0000 hourly 1 https://wordpress.org/?v=6.7.1 By: Ghenadie https://grey-panther.net/2010/03/string-intern-there-are-better-ways.html#comment-89 Sat, 01 Jan 2011 15:24:59 +0000 https://grey-panther.net/?p=113#comment-89 just 4 fun:
am dat din greseala cautare pe google dupa "string" si sa vezi ce am gasit la capitolul imagini: http://www.google.ro/images?q=string&um=1&ie=UTF-8&source=univ&ei=zGTcTMv-HJGTswb_7PyhBA&sa=X&oi=image_result_group&ct=title&resnum=4&ved=0CEsQsAQwAw&biw=1280&bih=666

;))

]]>
By: Cd-MaN https://grey-panther.net/2010/03/string-intern-there-are-better-ways.html#comment-149 Fri, 16 Apr 2010 11:06:01 +0000 https://grey-panther.net/?p=113#comment-149 @Markus: I forgot to mention that I've used the SUN JDK 1.6u18 for the measurements.

]]>
By: Cd-MaN https://grey-panther.net/2010/03/string-intern-there-are-better-ways.html#comment-150 Fri, 16 Apr 2010 11:05:16 +0000 https://grey-panther.net/?p=113#comment-150 @Markus: I found String.intern to be considerably slower (somewhere in the 2x – 5x range, I don't have the numbers at hand right now) than lookups in a hashmap.

Also, in many cases you can get away with a simple HashMap (for example if you have a read-only in-memory DB which is populated at the program startup).

But I completely agree that everyone should do his or her own measurement and do the most effective thing for her situation.

Best regards and thanks for the comment.

]]>
By: Unknown https://grey-panther.net/2010/03/string-intern-there-are-better-ways.html#comment-151 Fri, 16 Apr 2010 11:00:59 +0000 https://grey-panther.net/?p=113#comment-151 The advantage of using String.intern is that it requires less memory. A WeakHashmap has a high overhead (more than 100 bytes per entry IIRC). Weak references are also slowing down the GC and what you really would want a Concurrent StringWeakHashSet which would have an even higher overhead (most likely). String.intern is not perfect, but it works well enough on modern VM's.IIRC it's not slower at least not on the SAP JVM (a SUN Hotspot derivate).

]]>
By: Cd-MaN https://grey-panther.net/2010/03/string-intern-there-are-better-ways.html#comment-168 Thu, 25 Mar 2010 15:31:05 +0000 https://grey-panther.net/?p=113#comment-168 @Vita: String's in Java are immutable. This means that you can never modify their value (except using some dirty Reflection trickery, but you are not supposed to do that).

For example when you write the code:

String s = "aaaaa";
s = "bbbbbb" + s;

What actually happens is that you modify the string S points to (remember, in Java all non-primitive type variables are in fact references), not the actual value of the string.

Or to put it an other way: after the second line you will have to memory areas: one representing the string "aaaaa" and for the string "bbbbbbaaaaa" (at least until the GC kicks in and eliminates the first). What you are actually doing is re-pointing the reference s from the first to the second one.

]]>
By: Vita Tänder https://grey-panther.net/2010/03/string-intern-there-are-better-ways.html#comment-169 Thu, 25 Mar 2010 15:26:02 +0000 https://grey-panther.net/?p=113#comment-169 When do you use Intern or something similar to it? What is a practical application for this? I'm thinking if you have 2 strings with equal value and only one copy is stored, what happens if another process or thread modifies the string, while yet another thread tried to retrieve the string value and now you get a different one.

]]>