Java 1.4.2, I need a set of unique strings

D

Donkey Hottie

First, try it without doing anything special, you might be prematurely
worrying about a problem that isn't really a problem.

Second, if you find it really is a problem, you can try using
intern().

String parsedString = getParsedString();
String s = parsedString.intern();

I used HashMap, and not intern(), as it is a J2EE application, and I choose
not to pollute the specific heap for classes and interned strings with my
possibly huge number of things. A local HashMap is easier to discard and
thus gc.

I read mixed opinions about intern() in web, and chose to be sure.
 
M

Mike Schilling

Roedy said:
they are no the same via == but they are via equals.

I'm sure we've been through this at least 17 times in this thread, but
in this specific case (two identical constant strings), they will in
fact be the same when checked with ==, the reason being that constant
strings are interned.
 
S

scuzwalla

they are no the same via == but they are via equals.

Equal string literals will be the same via ==, actually.

Equal strings in general frequently won't be, though.
 
R

Roedy Green

I'm sure we've been through this at least 17 times in this thread, but
in this specific case (two identical constant strings), they will in
fact be the same when checked with ==, the reason being that constant
strings are interned.

I explain that in the essay I referred the OP to
http://mindprod.com/jgloss/interned.html

I did not take the example literally. I took it to mean she had two
variables that each pointed to a the same sequence of letters.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top