资源描述:
《rethinking java stringsnew》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、RethinkingJavaStringsPaoloBoldiSebastianoVignaDipartimentodiScienzedell'InformazioneUniversit`adegliStudidiMilano,ItalyAbstractTheJavastringclasses,StringandStringBuffer,lieattheextremesofaspectrum(immutable,reference-basedandmutable,content-based).Motivatedbydata-intensivetextapplications,weprop
2、oseanewstringclass,MutableString,whichtriestoembodythebestofbothapproaches.11IntroductionTheJavastringclasses,StringandStringBuffer,lieattheextremesofaspectrum(immutable,reference-basedandmutable,content-based).However,inseveralapplicationsthisdichotomyresultsininefficientobjecthandling,andinparti
3、cularinthecreationofmanyuselesstemporaryobjects.Inverylargedataapplications,withmillionsofaliveobjects,thecostofthisinefficiencymaybeexceedinglyhigh.Anotherproblemraiseswithatypicalstringusagepattern;inthiscommonscenario,youstartwithamutablestringobject,ofayet-unknownlength,andappend,delete,insert
4、andsubstitutecharacters;atacertainpointintime,youendupwithastringthatwillnotbechangedthereafter,andyouwouldliketo“freeze”itsstate.ToreplicatethisscenariousingthestandardJavastringclasses,youwillmostprobablyuseaString-Bufferinthefirstphase,andthenturnitintoaString,byusingthetoStringmethodofStringBu
5、ffer.Unfortunately,thefirstphasewillbeseverelysloweddownbythesynchronisationofStringBuffermeth-ods,whereasthemajorityofapplicationswillnotneedsynchronisationatall(orwillaccommodatetheirsynchronisationneedsatahigherlevel).Moreover,turningtheStringBufferintoaStringimpliesthecreationofanewobject.Ofco
6、urse,onemightsimplydecidenottoturntheStringBufferintoaString,butthismakesitimpossibletouseitinthesameoptimisedwayasanimmutablestring;evenworse,itisimpossibletouseaStringBufferinacollection,asitdoesnotoverridetheequals()methodprovidedbyObject.ThisdissatisfactionwiththebehaviourofStringandStringBuf
7、feriswellknownintheJavacom-munity.Forinstance,theAltavistacrawler,Mercator[5],hasbeenwritteninJava,buttheauthorsadmitthatoneoftheirfirststepswasrewritingthestandardJavastringclasses.Theauthorshavealsoexperiencedsimilart