第6章字符串
String是类,不是数组,也不是基本数据类型
String定义为final class,使用了copy on write技术
1)String的初始化
String str = new String(“hello”);
String str = “hello”;
2) String对象和String引用
String str1 = new String(“hello”);
String str2 = new String(“hello”);
boolean b = (str1==str2);
boolean b = (str2);
3)new和直接赋值的区别
String str1 = “hello”;
String str2 = “hello”;
boolean b = (str1==str2);
boolean b = (str2);
String VS StringBuffer
当字符串不大会改变的时候使用String,当字符串经常发生变化的时候使用StringBuffer:String 实际上都是常量,字符串的内容永远不会改变;StringBuffer可以为字符预留空间,因此是为经常变化的字符串考虑的
public class Test { public static void stringReplace (String text) { text = ('j' , 'i'); } public static void bufferReplace (StringBuffer text) { text = ("C"); } public static void main (String args[]) { String textString = new String ("java"); StringBuffer textBuffer = new StringBuffer ("java"); stringReplace (textString); bufferReplace (textBuffer); (textString + textBuffer); } }
public class E6 { public static void main(String args[]) { String s1="a"; String s2=new String("a"); String s3="A"; ((s2)); ((s3)); ((s3)); }}
public class E6 { public static void main(String args[]) { StringBuffer s1=new StringBuffer("a"); StringBuffer s2=new StringBuffer("a"); ((s2)); }}
public class E6 { public static void main(String args[]) { String s1="a"; String s2="a"; String s3=new String("a"); String s4=new String("a"); (s1==s2); (s3==s4); (s1==s3); } }
public class E6 { public static void main(String args[]) { String s1="a"; StringBuffer sb1=new StringBuffer("a"); StringBuffer sb2=sb1; String s2="a"+"b"; ("b"); (s1==s2); (sb
java字符串 来自淘豆网m.daumloan.com转载请标明出处.