Java课件第二章补充-字符串

上传人:痛*** 文档编号:252857146 上传时间:2024-11-20 格式:PPT 页数:43 大小:297KB
返回 下载 相关 举报
Java课件第二章补充-字符串_第1页
第1页 / 共43页
Java课件第二章补充-字符串_第2页
第2页 / 共43页
Java课件第二章补充-字符串_第3页
第3页 / 共43页
点击查看更多>>
资源描述
单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,2021/7/13,*,I Constructing a String,Chapter 2 The String Class,II String Methods,2021/7/13,1,The,String,Class,Constructing a String:,Obtaining String length and Retrieving Individual Characters in a string String,String Concatenation(,concat,),Substrings(substring(index),substring(start,end),Comparisons(equals,compareTo),String Conversions,Finding a Character or a Substring in a String,Conversions between Strings and Arrays,Converting Characters and Numeric Values to Strings,2021/7/13,2,Constructing Strings,Format:String,newString,=new,String(stringLiteral,);,String message=“,stringLiteral,;,Example:String message=new String(“Hello);,String message=Welcome to Java;,2021/7/13,3,Storage,String pool&stack&heap,String pool,(,字符串池,),:JVM has a String pool,it saves a lot of String objects that created by shorthand,initializer,.Data sharing,Stack,(栈),:Store primitive data,(,char,、,byte,、,short,、,int,、,long,、,float,、,double,、,boolean,),and constructor.Data sharing.,heap,(堆),:,Store Object.,2021/7/13,4,String,str,=new,String(abc,);,Difference between new and shorthand initializer,Question:How many String objects are created?,Two,Constructor:,publicString(Stringoriginal),/othercode.,Note:The parameter is a String object,2021/7/13,5,Stringa=,abc,;,String b=,abc,;,Difference between new and shorthand initializer,Question:How many String objects are created?,One,2021/7/13,6,A new object is created if you use the new operator.,If you use the string initializer,no new object is created if the interned object is already created.,Note,2021/7/13,7,Strings Are Immutable,A String object is immutable;its contents cannot be changed.,Does the following code change the contents of the string,String s=Java;,s=HTML;,?,2021/7/13,8,s:String,String object for“Java”,0 x1245,s,Trace Code,String s=Java;,s=HTML;,Contents can not be changed,After executing s=“java”;,s:String,String object for“Java”,0 x1245,s,After executing s=“HTML”;,s:String,String object for“HTML”,2021/7/13,9,Canonical Strings(规范字符串),canonical string:,if two String objects were created with,the same string literal,using the,shorthand initializer,then the JVM stores them in the same object.This string literal is called,canonical string,.,improve efficiency and save memory,U,sing,String,objects,intern,method to return a canonical string,2021/7/13,10,intern:,public native,String,intern(),Returns a canonical representation for the string object.,If s and t are strings such that s.equals(t),it is guaranteed that,s.intern()=t.intern().,Canonical Strings,2021/7/13,11,String s=Welcome to Java;,String s1=new String(Welcome to Java);,String s2=s1.intern();,String s3=Welcome to Java;,System.out.println(s1=s is +(s1=s);,System.out.println(s2=s is +(s2=s);,System.out.println(s=s3 is +(s=s3);,:String,Canonical string object for Welcome to Java,:String,A string object for Welcome to Java,Examples,display,s1=s is false,s2=s is true,s=s3 is true,2021/7/13,12,Finding String Length,Finding string length using the,length(),method:,Example:message=Welcome;,message.length(),(returns 7),2021/7/13,13,Retrieving Individual Characters in a String,Do not use,message0,Use,message.charAt(index,),public char,charAt(int,index),Returns the character at the specified index.An index ranges from 0 to length()-1.,Index starts from,0,charAt方法的使用,2021/7/13,14,public String concat(String str),Concatenates the specified string to the end of this string.,If the length of the argument string is 0,then this object is returned.,Parameters:the String that is concatenated to the end of this String.,Returns:a string that represents the concatenation of this objects characters followed by the string arguments characters.,String Concatenation,2021/7/13,15,String Concatenation,String s3=,s1.concat(s2),;,String s3=s1+s2;,s1+s2+s3+s4+s5 same as,(s1.concat(s2).concat(s3).concat(s4).concat(s5);,2021/7/13,16,public String substring(int beginIndex),Returns a new string that is a substring of this string.The substring begins at the specified index and extends to the end of this string.,public String substring(int beginIndex,int endIndex),Returns a new string that is a substring of this string.The substring begins at the specified beginIndex and extends to the character at index endIndex-1.,Parameters:the beginning index,inclusive.,Returns:the specified substring.,Throws:StringIndexOutOfBoundsException if the beginIndex is out of range.,Extracting Substrings,2021/7/13,17,Extracting Substrings,String is an,immutable,class;its valuescannot be changed individually.,String s1=Welcome to Java;,String s2=,s1.substring(0,11),+HTML;,2021/7/13,18,String Comparisons,equals,String s1=new String(Welcome“);,String s2=“Welcome;,if(s1.equals(s2),/s1 and s2 have the same contents,if(s1=s2),/s1 and s2 have the same reference,2021/7/13,19,String Comparisons,cont.,compareTo(Object object),String s1=new String(Welcome“);,String s2=welcome;,if(pareTo(s2)0),/s1 is greater than s2,else if(pareTo(s2)=0),/s1 and
展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 管理文书 > 施工组织


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!