java基础面试题-String深入理解

String实现源码

在这里插入图片描述
在java6之前,String对象主要有四个成员变量:char[]数组,offset偏移量,count字符数量,hash哈希值;通过offset和count两个属性可以定位char[]数组,共享数组对象,但是有可能会导致内存泄露。

泄露原因:调用subString获取小段字符串时,会共享原String对象,如果subString的对象一直被引用,且原String对象非常大,就会导致String对象的字符串一直无法被GC,出现内存泄露。

java7/8去掉了offset和count属性,同时修复了subString方法的bug。
java9中维护了一个新的属性coder,标识字符串的字节编码;char[]改成byte[],可以减少每一个字符的占用空间,由16字节减少为8字节。

截取部分java8字符串源码

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final char value[];

    /** Cache the hash code for the string */
    private int hash; // Default to 0

    /** use serialVersionUID from JDK 1.0.2 for interoperability */
    private static final long serialVersionUID = -6849794470754667710L;
    ......
    }
    public String substring(int beginIndex) {
        if (beginIndex < 0) {
            throw new StringIndexOutOfBoundsException(beginIndex);
        }
        int subLen = value.length - beginIndex;
        if (subLen < 0) {
            throw new StringIndexOutOfBoundsException(subLen);
        }
        return (beginIndex == 0) ? this : new String(value, beginIndex, subLen);
    }

不可变性的好处

  • 不可变对象不会被恶意修改,所以多线程共享时是线程安全的。
  • hash属性值一旦确定,不会被变更,确保唯一性。
  • 可以节约内存,实现字符串常量池。
    String str = “abc”,String str = new String(“abc”)的区别

String str = "abc"的方式会检查对象是否在字符串常量池中,如果在,就直接返回该对象的引用;否则新的字符串将在常量池中被创建。
str = new String(“abc”)每次都会在堆中新建一个对象。

String使用优化

字符串常量定义

字符串常量,使用String str = “test str”的方式来定义,String str = “a” + “b” + “cc”,字符串常量的拼接编译器会自动优化成String str = “abcc”,但字符串变量的拼接则不是如此
例如:

String str = "haha";

for(int i=0; i<10; i++) {
      str = str + i;
}

编译器会自动优化成:


String str = "haha";

for(int i=0; i<10; i++) {
        	  str = (new StringBuilder(String.valueOf(str))).append(i).toString();
}

这样在循环体内一直生成新的StringBuilder对象,性能是比较低的,这种情况下,最好在循环外层定义一个StringBuilder对象,然后使用该对象进行字符串的拼接。

String.intern大有可为

调用String的intern方法,会检查字符串常量池中是否有等于该对象的字符串,如果没有,就在常量池中新增该对象,并返回对象的引用;如果有,就返回常量池中字符串的引用。
通俗点说,针对某个字符串常量,大家是共用常量池中的字符串常量的。这个优化,导致的内存空间节约可能是巨大的
举个例子:
我们有个居民信息管理系统,存储的信息涉及到每个居民的省份,城市等,对应每个person的关键字段有String province, String city,假设居民数量是10亿,province和city的平均占用空间分别是10字节和5字节。
不使用String.intern时,占用的空间可能就是10亿15(字节)
==如果使用String.intern,所有居民共用同一份常量池里的字符串资源。如果全国所有省市都遍历完的话,大概占用空间为15
40(省的个数)*100(每个省城市的个数),差的可不是一两个数量级。==

String经典问题

对象地址是否相同

有了上面的基础,判断定义字符串的地址是否相同就比较容易了。

    public static void main(String[] args) {
        String str1= "abc";
        String str2= new String("abc");
        String str3= str2.intern();
        System.out.println((str1 == str2));
        System.out.println((str2 == str3));
        System.out.println((str1 == str3));
    }
//输出
false
false
true

String、StringBuffer、StringBuilder区别

  • String,定义字符串常量,每次对字符串的修改,都会返回一个新的字符串对象。如果涉及到字符串变量的拼接,不建议使用String。
  • StringBuilder,主要用于字符串变量的拼接,性能比String的拼接要高。线程不安全。适用于单线程或没有线程安全问题的字符串变量拼接。
  • StringBuffer,与StringBuilder类似,不过StringBuffer是线程安全的,也就是说任何对字符串的操作,都是加锁的,所以性能比StringBuilder低。适用于多线程下字符串变量的修改。
发布了35 篇原创文章 · 获赞 81 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/chanllenge/article/details/90645426
今日推荐