java语言程序设计基础篇(第八版)课件PPT第九章 机械工业出版报社 李娜译

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.01321308071Chapter9StringsandTextI/OLiang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.01321308072MotivationsOftenyouencountertheproblemsthatinvolvestringprocessingandfileinputandoutput.Supposeyouneedtowriteaprogramtoreplacealloccurrencesofawordwithanewwordinafile.Howdoyousolvethisproblem?Thischapterintroducesstringsandtextfiles,whichwillenableyoutosolvethisproblem.Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.01321308073ObjectivesTousetheStringclasstoprocessfixedstrings(§9.2).TousetheCharacterclasstoprocessasinglecharacter(§9.3).TousetheStringBuilder/StringBufferclasstoprocessflexiblestrings(§9.4).TodistinguishamongtheString,StringBuilder,andStringBufferclasses(§9.2-9.4).Tolearnhowtopassargumentstothemainmethodfromthecommandline(§9.5).TodiscoverfilepropertiesandtodeleteandrenamefilesusingtheFileclass(§9.6).TowritedatatoafileusingthePrintWriterclass(§9.7.1).ToreaddatafromafileusingtheScannerclass(§9.7.2).(GUI)Toopenfilesusingadialogbox(§9.8).Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.01321308074TheStringClassConstructingaString:–Stringmessage=WelcometoJava“;–Stringmessage=newString(WelcometoJava“);–Strings=newString();ObtainingStringlengthandRetrievingIndividualCharactersinastringStringConcatenation(concat)Substrings(substring(index),substring(start,end))Comparisons(equals,compareTo)StringConversionsFindingaCharacteroraSubstringinaStringConversionsbetweenStringsandArraysConvertingCharactersandNumericValuestoStringsLiang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.01321308075ConstructingStringsStringnewString=newString(stringLiteral);Stringmessage=newString(WelcometoJava);Sincestringsareusedfrequently,Javaprovidesashorthandinitializerforcreatingastring:Stringmessage=WelcometoJava;Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.01321308076StringsAreImmutableAStringobjectisimmutable;itscontentscannotbechanged.Doesthefollowingcodechangethecontentsofthestring?Strings=Java;s=HTML;Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.01321308077TraceCodeStrings=Java;s=HTML;:StringStringobjectforJavasAfterexecutingStrings=Java;Afterexecutings=HTML;:StringStringobjectforJava:StringStringobjectforHTMLContentscannotbechangedThisstringobjectisnowunreferencedsanimationLiang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.01321308078TraceCodeStrings=Java;s=HTML;:StringStringobjectforJavasAfterexecutingStrings=Java;Afterexecutings=HTML;:StringStringobjectforJava:StringStringobjectforHTMLContentscannotbechangedThisstringobjectisnowunreferencedsanimationLiang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.01321308079InternedStringsSincestringsareimmutableandarefrequentlyused,toimproveefficiencyandsavememory,theJVMusesauniqueinstanceforstringliteralswiththesamecharactersequence.Suchaninstanceiscalledinterned.Forexample,thefollowingstatements:Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.013213080710Examplesdisplays1==sisfalses1==s3istrueAnewobjectiscreatedifyouusethenewoperator.Ifyouusethestringinitializer,nonewobjectiscreatediftheinternedobjectisalreadycreated.Strings1=WelcometoJava;Strings2=newString(WelcometoJava);Strings3=WelcometoJava;System.out.println(s1==s2is+(s1==s2));System.out.println(s1==s3is+(s1==s3));:StringInternedstringobjectforWelcometoJava:StringAstringobjectforWelcometoJavas1s2s3Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.013213080711TraceCodeStrings1=WelcometoJava;Strings2=newString(WelcometoJava);Strings3=WelcometoJava;:StringInternedstringobjectforWelcometoJavas1animationLiang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.013213080712TraceCodeStrings1=WelcometoJava;Strings2=newString(WelcometoJava);Strings3=WelcometoJava;:StringInternedstringobjectforWelcometoJava:StringAstringobjectforWelcometoJavas1s2Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.013213080713TraceCodeStrings1=WelcometoJava;Strings2=newString(WelcometoJava);Strings3=WelcometoJava;:StringInternedstringobjectforWelcometoJava:StringAstringobjectforWelcometoJavas1s2s3Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.013213080714StringComparisonsjava.lang.String+equals(s1:String):boolean+equalsIgnoreCase(s1:String):boolean+compareTo(s1:String):int+compareToIgnoreCase(s1:String):int+regionMatches(toffset:int,s1:String,offset:int,len:int):boolean+regionMatches(ignoreCase:boolean,toffset:int,s1:String,offset:int,len:int):boolean+star

1 / 58
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功