您的位置:首页 > 产品设计 > UI/UE

java String StringBuffer Stringbuilder

2017-10-22 14:40 483 查看

String

String的实例化

String str = "hello"; //1.直接赋值
String str1 = new String("hello"); // 2.通过new关键字


二者差别:

直接赋值:

public static void main(String[] args) {
String stra = "hello" ;
String strb = "hello" ;
String strc = "hello" ;
System.out.println(stra == strb);//true
System.out.println(stra == strc);//true
System.out.println(strb == strc);//true
}
}




通过new关键字

public class StringDemo {

public static void main(String[] args) {
String stra = new String("hello");
String strb = "hello";
System.out.println(stra == strb);//false

}
}




注意:通过new关键字:会开辟两块堆内存空间,其中有一块空间将成为垃圾,并且不会自动入池,但是用户可以使用intern()自动入池

手动入池:通过String类定义的public String intern()方法

public static void main(String[] args) {
String stra = new String("hello").intern();
String strb = "hello";
System.out.println(stra == strb);//true
}


因此在实际工作使用直接赋值的方法!!!

String一旦定义则不可更改

public class StringDemo {

public static void main(String[] args) {
String str = "Hello";
str = str + "World";
str += "!!!";
System.out.println(str);

}
}




因此,字符串并没有改变,而对于字符串内容的变化,则是利用了引用关系的变化实现的,但是每次改变都会产生垃圾空间,因此应减少对字符串的修改!!!

StringBuffer

认识StringBuffer

缓冲区,本身也是操作字符串,但是与String不同,StringBuffer是可以更改的

StringBuffer是一个操作类,所以必须通过实例化进行操作

public static void main(String[] args) {
StringBuffer s1 = new StringBuffer();
s1.append("i");
System.out.println(s1); //输出结果:i
s1.append(" love");
System.out.println(s1); //输出结果:i love
s1.append(" you");
System.out.println(s1); //输出结果:i love you
}


对比:



StringBuffer常用方法

追加:append()

插入:insert()

替换:replace()

返回查询字符串的位置,无则返回-1:indexOf()

public static void main(String[] args) {

StringBuffer buff = new StringBuffer("programming language");

// returns the index of the specified substring
System.out.println("Index of substring  = " + buff.indexOf("age"));

// returns -1 as substring is not found
System.out.println("Index of substring  = " + buff.indexOf("k"));


StringBuilder

认识StringBuilder

一个可变序列,该类被设计作为StringBuffer的简易替换,用在字符串缓冲区被单个线程使用的时候。建议优先考虑此类,速度比StringBuffer快。但是考虑到线程安全方面,建议使用StringBuffer。

常用方法

追加:append()

插入:insert()

String StringBuffer Stringbuilder比较

是否可以修改

String 不可以修改

StringBuffer Stringbuilder 可以修改

速度

StringBuilder > StringBuffer > String

String:字符串常量

StringBuffer:字符串变量

StringBuilder:字符串变量

String每修改一次就会开辟一次内存,然后在引用,所以速度慢!

注意:

String str = “This is only a” + “ simple” + “ test”;
StringBuffer builder = new StringBuilder(“This is only a”).append(“ simple”).append(“ test”);


这里str生成速度比builder快,原因:

String str = “This is only a” + “ simple” + “test”;

其实就是:

String str = “This is only a simple test”;

StringBuilder与 StringBuffer

StringBuilder:线程非安全的

StringBuffer:线程安全的

  当我们在字符串缓冲去被多个线程使用是,JVM不能保证StringBuilder的操作是安全的,虽然他的速度最快,但是可以保证StringBuffer是可以正确操作的。当然大多数情况下就是我们是在单线程下进行的操作,所以大多数情况下是建议用StringBuilder而不用StringBuffer的,就是速度的原因。

String StringBuffer Stringbuilder选取

1.如果要操作少量的数据用 = String

2.单线程操作字符串缓冲区 下操作大量数据 = StringBuilder

3.多线程操作字符串缓冲区 下操作大量数据 = StringBuffer
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java string 实例