Hi Rodrigo,

using intern() will save space, but not time, as the method
internally performs equals() on the string pool contents
(check JavaDoc for details), either returning a reference
if some string with the same contents is already in the
pool; if not, the string is added and its own refe-
rence is returned. It's true that you may later check
for equality by using '==', just like that is possible
in case of a string constant. Don't really know if I
would use that approach, actually, up to now, I
never did. The two Golden Rules about the String
class I learned are 1. Strings are immutable and 2.
always use equals() and not "==", not even when
comparing constants (who would do such a thing
anyway?) For the Java exam, you'll have to know
details about the string pool, but probably just
to help you in avoiding mistakes, and they teach
you otherwise, anyway. Finally, at least Cay
Horstmann (author of Core Java, Sun Press)
explicity warns from using "==", because the
string pool feature apparently is implementation
specific and not guaranteed to work every-
where. Don't know, as I know that many ad-
vanced developers make use of intern(), but
still, up to now I've never seen one of them
comparing strings via operators instead of
equals().

Best regards,
-- Chris (SCPJ2)

----- Original Message -----
From: "Rodrigo Ruiz Aguayo" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Saturday, June 22, 2002 8:02 PM
Subject: Re: If..else issue


> Only a little comment :-)
>
> When I have to make many string comparisons just like in this code
fragment, I prefer using the intern() method of the String class.
>
> The code changes to:
>
> String s = request.getParameter("collection");
> s = s.intern();
>
> // Now comparisons can be made through ==
>
> if (s == "scopus") {
> ...
> }
> else if (s == "securetrak") {
> ...
> }
> else ...
>
> String comparisons are expensive. If the number of comparisons is large
enough, the cost of the intern() call becomes insignificant.
> Of course, I am speaking about more than two comparisons. Anybody have
made some tests? I have never had time enough. I use this when I have to do
more that ten comparisons, but I suppose it can provide benefits with less
comparisons.
>
>
___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
>
>

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to