资源描述
单击此处编辑母版标题样式,单击此处编辑母版文本样式,第二级,第三级,第四级,第五级,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
展开阅读全文