String,StringBuilder,StringBuffer区别

String类是final类,也即意味着String类不能被继承,并且它的成员方法都默认为final方法

只有在确定不想让该方法被覆盖时,才将方法设置为final

无论是sub操、concat还是replace操作都不是在原有的字符串上进行的,而是重新生成了一个新的字符串对象。也就是说进行这些操作后,最原始的字符串并没有被改变。

“对String对象的任何改变都不影响到原对象,相关的任何change操作都会生成新的对象”

public class Main {
 
    public static void main(String[] args) {
        String str1 = "hello world";
        String str2 = new String("hello world");
        String str3 = "hello world";
        String str4 = new String("hello world");
 
        System.out.println(str1==str2);false
        System.out.println(str1==str3);true
        System.out.println(str2==str4);false
    }
}

通过new关键字来生成对象是在堆区进行的,而在堆区进行对象生成的过程是不会去检测该对象是否已经存在的。因此通过new来创建对象,创建出的一定是不同的对象,即使字符串的内容是相同的。

 

String、StringBuffer以及StringBuilder的区别

public static void main(String[] args) {
        String string = "";
        for(int i=0;i<10000;i++){
            string += "hello";
        }
    }

string += “hello”;的过程相当于将原有的string变量指向的对象内容取出与”hello”作字符串相加操作再存进另一个新的String对象当中,再让string变量指向新生成的对象。

每次循环会new出一个StringBuilder对象,然后进行append操作,最后通过toString方法返回String对象。也就是说这个循环执行完毕new出了10000个对象,试想一下,如果这些对象没有被回收,会造成多大的内存资源浪费。

string += "hello" 
 等同于
 StringBuilder str = new StringBuilder(string);

str.append(“hello”);

str.toString();

 

public static void main(String[] args) {
        StringBuilder stringBuilder = new StringBuilder();
        for(int i=0;i<10000;i++){
            stringBuilder.append("hello");
        }
    }

这段代码append操作是在原有对象的基础上进行的,每次循环并没有new

 

那么有人会问既然有了StringBuilder类,为什么还需要StringBuffer类?

StringBuilder和StringBuffer类拥有的成员属性以及成员方法基本相同,区别是StringBuffer类的成员方法前面多了一个关键字:synchronized,

1)对于直接相加字符串,效率很高,因为在编译器便确定了它的值,也就是说形如”I”+”love”+”java”; 的字符串相加,在编译期间便被优化成了”Ilovejava”。这个可以用javap -c命令反编译生成的class文件进行验证。

对于间接相加(即包含字符串引用),形如s1+s2+s3; 效率要比直接相加低,因为在编译器不会对引用变量进行优化。

当字符串相加操作或者改动较少的情况下,建议使用 String str=”hello”这种形式;

当字符串相加操作较多的情况下,建议使用StringBuilder,如果采用了多线程,则使用StringBuffer。

常见的关于String、StringBuffer的面试题

1. 下面这段代码的输出结果是什么?

String a = “hello2″;   String b = “hello” + 2;   System.out.println((a == b));

输出结果为:true。原因很简单,”hello”+2在编译期间就已经被优化成”hello2″,因此在运行期间,变量a和变量b指向的是同一个对象。

2.下面这段代码的输出结果是什么?

String a = “hello2″;   String b = “hello”; String c = b + 2; System.out.println((a == c));

输出结果为:false。由于有符号引用的存在,所以 String c = b + 2;不会在编译期间被优化,不会把b+2当做字面常量来处理的,因此这种方式生成的对象事实上是保存在堆上的。因此a和c指向的并不是同一个对象。javap -c得到的内容:

3.下面这段代码的输出结果是什么?

String a = “hello2″;   final String b = “hello”; String c = b + 2; System.out.println((a == c));

输出结果为:true。对于被final修饰的变量,会在class文件常量池中保存一个副本,也就是说不会通过连接而进行访问,对final变量的访问在编译期间都会直接被替代为真实的值。那么String c = b + 2;在编译期间就会被优化成:String c = “hello” + 2;

 

public static void main(String[] args) {
        String a = "hello";
        String b = new String("hello");
        String c = new String("hello");
        String d = b.intern();

        System.out.println(a==b);false
        System.out.println(b==c);false
        System.out.println(b==d);false
        System.out.println(a==d);true

    }

intern方法是一个本地方法,在JAVA SE6之前,intern方法会在运行时常量池中查找是否存在内容相同的字符串,如果存在则返回指向该字符串的引用,如果不存在,则会将该字符串入池,并返回一个指向该字符串的引用。因此,a和d指向的是同一个对象。

String str = new String(“abc”)涉及到几个String对象?合理的解释是2个。

posted @ 2018-11-27 00:17  箜篌nicole  阅读(167)  评论(0编辑  收藏  举报