java语言程序设计-基础篇--第八版--英文课件(第9章)

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

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

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

资源描述

Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807Chapter9StringsandTextI/O1Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807MotivationsOftenyouencountertheproblemsthatinvolvestringprocessingandfileinputandoutput.Supposeyouneedtowriteaprogramtoreplacealloccurrencesofawordwithanewwordinafile.Howdoyousolvethisproblem?Thischapterintroducesstringsandtextfiles,whichwillenableyoutosolvethisproblem.2Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807Objectives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).3Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807TheStringClassConstructingaString:Stringmessage=WelcometoJava“;Stringmessage=newString(WelcometoJava“);Strings=newString();ObtainingStringlengthandRetrievingIndividualCharactersinastringStringConcatenation(concat)Substrings(substring(index),substring(start,end))Comparisons(equals,compareTo)StringConversionsFindingaCharacteroraSubstringinaStringConversionsbetweenStringsandArraysConvertingCharactersandNumericValuestoStrings4Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807ConstructingStringsStringnewString=newString(stringLiteral);Stringmessage=newString(WelcometoJava);Sincestringsareusedfrequently,Javaprovidesashorthandinitializerforcreatingastring:Stringmessage=WelcometoJava;5Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807StringsAreImmutableAStringobjectisimmutable;itscontentscannotbechanged.Doesthefollowingcodechangethecontentsofthestring?Strings=Java;s=HTML;6Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807TraceCodeStrings=Java;s=HTML;7:StringStringobjectforJavasAfterexecutingStrings=Java;Afterexecutings=HTML;:StringStringobjectforJava:StringStringobjectforHTMLContentscannotbechangedThisstringobjectisnowunreferencedsanimationLiang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807TraceCodeStrings=Java;s=HTML;8:StringStringobjectforJavasAfterexecutingStrings=Java;Afterexecutings=HTML;:StringStringobjectforJava:StringStringobjectforHTMLContentscannotbechangedThisstringobjectisnowunreferencedsanimationLiang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807InternedStringsSincestringsareimmutableandarefrequentlyused,toimproveefficiencyandsavememory,theJVMusesauniqueinstanceforstringliteralswiththesamecharactersequence.Suchaninstanceiscalledinterned.Forexample,thefollowingstatements:9Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807Examplesdisplays1==sisfalses1==s3istrue10Anewobjectiscreatedifyouusethenewoperator.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.0132130807TraceCode11Strings1=WelcometoJava;Strings2=newString(WelcometoJava);Strings3=WelcometoJava;:StringInternedstringobjectforWelcometoJavas1animationLiang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807TraceCode12Strings1=WelcometoJava;Strings2=newString(WelcometoJava);Strings3=WelcometoJava;:StringInternedstringobjectforWelcometoJava:StringAstringobjectforWelcometoJavas1s2Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807TraceCode13Strings1=WelcometoJava;Strings2=newString(WelcometoJava);Strings3=WelcometoJava;:StringInternedstringobjectforWelcometoJava:StringAstringobjectforWelcometoJavas1s2s3Liang,IntroductiontoJavaProgramming,EighthEdition,(c)2011PearsonEducation,Inc.Allrightsreserved.0132130807StringComparisons14java.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

×
保存成功