欢迎来到天天文库
浏览记录
ID:58882945
大小:289.00 KB
页数:42页
时间:2020-09-30
《Java课件第二章补充 字符串.ppt》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、IConstructingaStringChapter2TheStringClassIIStringMethods1TheStringClassConstructingaString:ObtainingStringlengthandRetrievingIndividualCharactersinastringStringStringConcatenation(concat)Substrings(substring(index),substring(start,end))Comparisons(equals,compa
2、reTo)StringConversionsFindingaCharacteroraSubstringinaStringConversionsbetweenStringsandArraysConvertingCharactersandNumericValuestoStrings2ConstructingStringsFormat:StringnewString=newString(stringLiteral);Stringmessage=“stringLiteral";Example:Stringmessage=ne
3、wString(“Hello");Stringmessage="WelcometoJava";3StorageStringpool&stack&heapStringpool(字符串池):JVMhasaStringpool,itsavesalotofStringobjectsthatcreatedbyshorthandinitializer.DatasharingStack(栈):Storeprimitivedata(char、byte、short、int、long、float、double、boolean)andco
4、nstructor.Datasharing.heap(堆):StoreObject.4Stringstr=newString("abc");DifferencebetweennewandshorthandinitializerQuestion:HowmanyStringobjectsarecreated?TwoConstructor:public String(String original) {//other code ...}Note:TheparameterisaStringobject5String a="a
5、bc";Stringb="abc";DifferencebetweennewandshorthandinitializerQuestion:HowmanyStringobjectsarecreated?One6Anewobjectiscreatedifyouusethenewoperator.Ifyouusethestringinitializer,nonewobjectiscreatediftheinternedobjectisalreadycreated.Note7StringsAreImmutableAStri
6、ngobjectisimmutable;itscontentscannotbechanged.DoesthefollowingcodechangethecontentsofthestringStrings="Java";s="HTML";?8s:StringStringobjectfor“Java”0x1245sTraceCodeStrings="Java";s="HTML";ContentscannotbechangedAfterexecutings=“java”;s:StringStringobjectfor“J
7、ava”0x1245sAfterexecutings=“HTML”;s:StringStringobjectfor“HTML”×9CanonicalStrings(规范字符串)canonicalstring:iftwoStringobjectswerecreatedwiththesamestringliteralusingtheshorthandinitializer,thentheJVMstorestheminthesameobject.Thisstringliteraliscalledcanonicalstrin
8、g.improveefficiencyandsavememoryUsingStringobject’sinternmethodtoreturnacanonicalstring10intern:publicnativeStringintern()Returnsacanonicalrepresentationforthestringobject.I
此文档下载收益归作者所有