您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 企业财务 > 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.0132130807ObjectivesTousetheStringclasstoprocessfixedstrings(§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.0132130807TheStringClassConstructingaString:Stringmessage=WelcometoJava“;Stringmessage=newString(WelcometoJava“);Strings=newString();ObtainingStringlengthandRetrievingIndividualCharactersinastringStringConcatenation(concat)Substrings(substring(index),substring(start,end))Comparisons(equals,compareTo)StringConversionsFindingaCharacteroraSubstringinaStringConversionsbetweenStringsandArraysConvertingCharactersandNumericValuestoStrings4Liang,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
本文标题:java语言程序设计-基础篇--第八版--英文课件(第9章)
链接地址:https://www.777doc.com/doc-2881528 .html