Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

compare String

Status
Not open for further replies.

never

Programmer
Dec 2, 2001
18
GB
Hi all,
today a read article about performance java programing and there is tips use better and fast technique how can compare String.

String a = new String("testing");
a = a.intern();
String b = new String("testing");
b = b.intern();
if(a == b )
System.out.println("equals");
else
System.out.println("fuck");

till today I used only a.equals(b) ... Know somebody technique used with intern() method ??
Can somebody clear up disadvantages ???

THX
Never

 
There are several methods for comparing Strings, but the way you do it is easyer. There is:

.equals()
and
.equalsIgnoreCase()
and
.compareTo()
and
.compareToIgnoreCase()
:)
"and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
Sure it may gain you an extra nanosecond of performance but is it worth it to further complicate the code? It is just going to cause future maintence problems when a developer that isn't aware of this technique comes across it and wonders what the heck you were thinking.

The equals() method works just as well and it leaves no question as to the nature of the code. This is truely self-documentation at its best.

So unless you absolutely need the extra nanosecond of performance then I suggest sticking with Jeff Langr's mantra of "make it work, make it right, make it fast". Jeff wrote a very good book on Java Programming Practices called Essential Java Style (ISBN: 0130850861), I highly recommend it. Wushutwist
 
Also, I don't know who wrote that article but I am starting to doubt its credibility. Upon further research (via Javadocs), it appears that the intern() method internally uses the equals() method. Having a look at the source for the String class confirms this. So you are not saving anything, in fact I would be of the opinion that you are actually getting worse performance since you would be calling intern() twice, hence two calls to equals() vs one call with the normal way.

Here is the JavaDocs so you can look for yourself:
Code:
intern

public String intern()


Returns a canonical representation for the string object.

A pool of strings, initially empty, is maintained privately by the class String.

When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned. Otherwise, this String object is added to the pool and a reference to this String object is returned.

It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.

All literal strings and string-valued constant expressions are interned. String literals are defined in §3.10.5 of the Java Language Specification

Returns:

a string that has the same contents as this string, but is guaranteed to be from a pool of unique strings.
Wushutwist
 
Thanks wushutwist. But I don't think it realy is that important.

By the way, did I realy do that bad of a job in replying to his question? :cool: "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
Details are always important. Your answer did fine on answering the HOW, I just filled in on the WHY. Wushutwist
 
ouuhh you're a little right and you're a little false :) I tested and when you use intern() it's more faster
coz you to compare use == and it's faster ...JVM inside use equals reference
 
ouuhh you're a little right and you're a little false :) I tested and when you use intern() it's more faster
coz you to compare use == and it's faster ...JVM inside use equals reference
My question was ....if somebody use intern and know some bad behaviour ..:)
 
You last sentence makes litte to no sence. :-( "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
I looked back and realized that intern is declared as a native method despite what the Javadocs say about it using equals(). I couldn't find the actual C code for intern() though. Then again if you are using a JIT or HOTSPOT (and who isn't) then most of your Java code will be native code anyways so after the first run I wouldn't expect much difference.

Also I refer back to my original post about maintainability. Not to mention it would be much easier to introduce bugs into your application using this comparison method for very little gain. Imagine one time if you forget to intern() a String. It is not going to be caught at compile time and certainly will not cause a Runtime Exception but the functionality will definitely not be what is expected. This is the type of bug that can be very hard to track down. Wushutwist
 
I'm with wushutwist on this one. :)

Besides, it's easyer. ;-) "and everything under the sun is in tune
but the sun is eclipsed by the moon." --Pink Floyd: Eclipse


"I'm going to spend eternity
reinstalling Windows." --Reinstalling Windows: by some British guy
 
Wushutwist ....you're right...this type of bugs are horrible ... it was only academic question if am i last programmer who don't use "intern()" ..
More inportat i find useful using, for example, String.append before " + " or BufferdReader before Reader:) here have speed more important function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top