东理子一构造字符串对象SHANDONGUNIVERSITYOFTECHNOLOGY1.常量对象:用双引号括起的字符序列二"你好”、"12.97”、“boy"等。2.字符串对象声明:String str;常用构造方法:String(String s);String (chara)String(char a,int startlndex,int count)s3.引用字符串常量对象0xAb28+How-are youStrings1=“Howareyou"OxAb28+Strings2=“Howareyou"$24专业专注创新
专业 专注 创新 一. 构造字符串对象 1.常量对象:用双引号括起的字符序列. "你好" 、"12.97"、 "boy"等。 2.字符串对象 声明:String str; 常用构造方法: String(String s); String (char a[]); String(char a[],int startIndex,int count) 3.引用字符串常量对象 String s1 = “How are you"; String s2 = “How are you";
东理字符串常量的维护StringPoolSHANDONGUNIVERSITY OFTECHNOLOGYJava执行程序时维护一个StringPool,对于可共享的字符串对象,先在Stringpool中查找是否存在,找到后返回,2而不是再创造一个新的String对象,以减少内存的耗用。字符串池存放:字符串常量:有限个字符串常量进行“+”运算的结果由类java.lang.String的成员方法internO创建的字符串实例对象publicstatic voidmain(Stringargs[])(String str1="adadaf";String str2="adadaf";System.out.println("str1==str2?"+(str1==str2));}/方法main结束intern英】int3.n美)Inta.n专业专注创新美住院实习医生:拘禁(俘虏等):扣留《船只等)n拘留,关押
专业 专注 创新 字符串常量的维护 String Pool Java执行程序时维护一个String Pool, 对于可共 享的字符串对象,先在String pool中查找是否存 在,找到后返回,而不是再创造一个新的String对 象,以减少内存的耗用。 字符串池存放: 字符串常量; 有限个字符串常量进行“+”运算的结果 由类java.lang.String的成员方法intern()创建的字符串 实例对象 public static void main(String args[ ]) { String str1="adadaf"; String str2="adadaf"; System.out.println("str1==str2? "+(str1==str2)); } // 方法main结束
东理功intern0方法SHANDONGUNIVERSITY OFTECHNOLOGY功能:如果StringPool中已经包括了相同内容的String对象,就从池中返回该字符串,否则原String对象会被加入池中,并返回这个String对象的引用。public staticvoid main(Stringargs[))VStringstr1="abcdefg"Stringstr2="hijklmn"Stringstr3="abcdefghijklmn"Stringstr4="abcdefg"+"hijklmn"str3==str4? trueSystem.out.println("str3==str4?"+(str3==str4)System.out.println("加入Pool前:");加入Poo1前:Stringstr5=null;1/非常量相加str5=str1+str2;str3==str5? falseSystem.out.println("str3==str5?"+(str3==str5);加入Poo1后:System.out.println("加入Pool后:");str5=str5.intern0;str3==str5? trueSystem.out.printin("str3==str5?"+(str3==str5);l方法main结束专业专注创新
专业 专注 创新 intern()方法 功能:如果String Pool中已经包括了相同内容的 String对象,就从池中返回该字符串,否则原 String对象会被加入池中,并返回这个String对象 的引用。 public static void main(String args[ ]) { String str1="abcdefg"; String str2="hijklmn"; String str3="abcdefghijklmn"; String str4="abcdefg"+"hijklmn"; System.out.println("str3==str4? "+(str3==str4)); System.out.println("加入Pool前:"); String str5=null; str5=str1+str2; //非常量相加 System.out.println("str3==str5? "+(str3==str5)); System.out.println("加入Pool后:"); str5=str5.intern(); System.out.println("str3==str5? "+(str3==str5)); } // 方法main结束
东理二.常用方法SHANDONGUNIVERSITYOFTECHNOLOGYpublicStringtoUpperCaseo使用默认语言环境的规则将此String中的所有字符都转换为大写。publicStringtoLowerCase)使用默认语言环境的规则将此String中的所有字符都转换为小写。public String concat(String str)将指定字符串连接到此字符串的结尾publiccharcharAt(intindex)返回指定索引处的char值。publicintlength()返回此字符串的长度专业专注创新
专业 专注 创新 public String toUpperCase() 使用默认语言环境的规则将此 String 中的所有字符都转换 为大写。 public String toLowerCase() 使用默认语言环境的规则将此 String 中的所有字符都转换 为小写。 public String concat(String str) 将指定字符串连接到此字符串的结尾。 public char charAt(int index) 返回指定索引处的 char 值。 public int length() 返回此字符串的长度。 二. 常用方法
东理客二.常用方法SHANDONG UNIVERSITYOFTECHNOLOGYpublic boolean equals(String s)比较当前字符串对象的实体是否与参数s指定的字符串的实体相同,public boolean equalslgnoreCase(String s)public boolean startsWith(String s)public boolean endsWith(String s)判断当前字符串对象的前缀(后缀)是否是参数s指定的字符串public boolean regionMatches(int firstStart,Stringother,intotherStart,intlength)从当前字符串参数firstStart指定的位置开始处,取长度为length的一个子串,并将这个子串和参数other指定的一个子串进行比较。★重载方法:publicbooleanregionMatches(booleanb,比较时int firstStart,String other,int ortherStart,int length)是否忽略字母大小写
专业 专注 创新 public boolean equals(String s) 比较当前字符串对象的实 体是否与参数s指定的字符串的实体相同. public boolean equalsIgnoreCase(String s) public boolean startsWith(String s) public boolean endsWith(String s) 判断当前字符串对象的 前缀(后缀)是否是参数s指定的字符串 public boolean regionMatches(int firstStart,String other,int otherStart,int length) 从当前字符串参数firstStart指定的位置开始处,取长度为 length的一个子串,并将这个子串和参数other指定的一个子 串进行比较。 重载方法:public boolean regionMatches(boolean b, int firstStart,String other,int ortherStart,int length) 比较时 是否忽略字母大小写 二. 常用方法