Java面试题(三)

四.== 与 equals 与 hashCode之间的关系

https://juejin.im/post/5a4379d4f265da432003874c#heading-12
https://www.cnblogs.com/Qian123/p/5703507.html

五.String&StringBuffer&StringBuilder

1.三者的异同点:
(1)这三者都是java常用的处理字符串的类,都是final修饰的,不允许被继承
(2)String对象是不可变的,也就是说,每次处理后都会生成一个新的对象,然后指向这个对象(如果字符串常量池中有修改后的对象就不用重新创建,直接指向那个对象);
StringBuffer对象是可变的,也就是说是对对象本身进行操作,本身是线程安全的;
StringBuilder对象和StringBuffer基本一致,但是StringBuilder是线程不安全的;
2.针对异同点的解释

package top.flame.test;

public class Test {
    public static void main(String[] args) {
        String a = "hello";
        String b = "hello";
        String c = new String("hello");
        String d = "helloworld".substring(0,5);
        String e = "he" + "llo";
        System.out.println("d : " + d);
        System.out.println("a == b : "+(a == b));
        System.out.println("a == c : "+(a == c));
        System.out.println("a == d : "+(a == d));
        System.out.println("a == e : "+(a == e));
    }
}

执行结果

d : hello
a == b : true
a == c : false
a == d : false
a == e : true

Process finished with exit code 0

(1)通过a和b比较可以看出来,a和b指向的是同一个对象;
(2)a和c的结果,是因为String c = new String(“hello”);可能创建一个对象,也可能创建两个;如果在字符串常量池中没有,会在常量池中创建一个,然后拷贝一份到heap中;如果常量池中有,直接在heap中拷贝一份;
(3)a和d的结果可以通过源码看,实际上和new String()是一样的,在最后返回的时候可以看到

    public String substring(int beginIndex, int endIndex) {
        if (beginIndex < 0) {
            throw new StringIndexOutOfBoundsException(beginIndex);
        }
        if (endIndex > value.length) {
            throw new StringIndexOutOfBoundsException(endIndex);
        }
        int subLen = endIndex - beginIndex;
        if (subLen < 0) {
            throw new StringIndexOutOfBoundsException(subLen);
        }
        return ((beginIndex == 0) && (endIndex == value.length)) ? this
                : new String(value, beginIndex, subLen);
    }

(4)a和e比较,实际上String e = “he” + “llo”;在编译过程中是有优化,这句代码和String e = “hello”;是一致的;

package top.flame.test;

public class Test {
    public static final String str = "hello";
    public static final StringBuffer buffer = new StringBuffer("hello");
    public static final StringBuilder builder = new StringBuilder("hello");

    public static void main(String[] args) {
        buffer.append("world");
        System.out.println("buffer = " + buffer.toString());
        builder.append("world");
        System.out.println("builder = " + builder.toString());
		//str = str + "world";
    }
}

(5)定义final的String对象后,当改变值的时候,实际上对象变了,所以会报错;当注释掉这句之后,执行结果如下,说明StringBuffer和StringBuilder的操作是针对对象本身操作的,所以即使是final修饰,还是能够改变值。

buffer = helloworld
builder = helloworld

Process finished with exit code 0

(6)在确定是单线程使用的时候可以是StringBuilder,效率要比StringBuffer快

	//StringBuffer添加锁
    @Override
    public synchronized StringBuffer append(String str) {
        toStringCache = null;
        super.append(str);
        return this;
    }
	//StringBuilder对象,没有加锁
    @Override
    public StringBuilder append(String str) {
        super.append(str);
        return this;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值