Tr�s int�ressant, ton article, beaucoup plus que la javadoc que je n'avais pas manqu� de consulter ... Donc on y lit :
 
public String intern() interns a String in the common string memory pool. Example: String potentialNapoleanQuote = new String ("Able was I, ere I saw Elba!"); potentialNapoleanQuote.intern ();.
To quicken string searches, use intern() to intern your Strings in the common string memory pool. Because that pool contains no duplicate Strings, each object has a unique reference. Plus, using == to compare references proves faster than using a method to compare a string's characters.
 
Il appara�t donc que prefix.intern() permet d'ajouter une cha�ne dans le pool de cha�nes sauvegard� en interne et d'acc�l�rer ensuite la comparaison de deux cha�nes en utilisant l'op�rateur == plut�t que la m�thode equals().

-----Message d'origine-----
De : marc [mailto:[EMAIL PROTECTED]
Envoy� : lundi 10 mars 2003 09:22
� : [EMAIL PROTECTED]
Objet : Re: String.intern()

un petit article mais qui en dit long sur votre question
http://www.javaworld.com/javaworld/jw-10-2002/jw-1004-java101_p.html
A+ marc

Yannick Emeriaud wrote:
Zeljko VELAJA a �crit :Je m'interroge sur ce que signifie :

  
            prefix = prefix.intern();
            uri = uri.intern();

Qu'est-ce que �a aurait chang� si ces deux lignes avaient �t� omises ?
    

Extrait de la doc de 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."
----------------------------------------------------------------------------------

L'int�r�t est donc d'avoir une unicit� sur l'�l�ment.

Qu'est-ce que �a aurait chang� si ces deux lignes avaient �t� omises ?
Des doublons dans le Vector declarations.

J'avoue que je ne connaissais pas cette m�thode et fonctionnement de
"String".
C'�tait donc une EXCELLENTE question que tu as pos�e !

A+
Yannick.





  

Répondre à