Java Foundation 3: In-depth understanding of String and wrapper classes

This section mainly introduces the difference between string constants and string types

The specific code can be found in my GitHub

https://github.com/h2pl/MyTech

The article was first published on my personal blog:

https://h2pl.github.io/2018/04/23/javase3

String concatenation

@Test
public void contact () {
    //1连接方式
    String s1 = "a";
    String s2 = "a";
    String s3 = "a" + s2;
    String s4 = "a" + "a";
    String s5 = s1 + s2;
    //表达式只有常量时,编译期完成计算
    //表达式有变量时,运行期才计算,所以地址不一样
    System.out.println(s3 == s4); //f
    System.out.println(s3 == s5); //f
    System.out.println(s4 == "aa"); //t

}

intern of type String

public void intern () {
    //2:string的intern使用
    //s1是基本类型,比较值。s2是string实例,比较实例地址
    //字符串类型用equals方法比较时只会比较值
    String s1 = "a";
    String s2 = new String("a");
    //调用intern时,如果s2中的字符不在常量池,则加入常量池并返回常量的引用
    String s3 = s2.intern();
    System.out.println(s1 == s2);
    System.out.println(s1 == s3);
}

equals of type String

//字符串的equals方法
//    public boolean equals(Object anObject) {
//            if (this == anObject) {
//                return true;
//            }
//            if (anObject instanceof String) {
//                String anotherString = (String)anObject;
//                int n = value.length;
//                if (n == anotherString.value.length) {
//                    char v1[] = value;
//                    char v2[] = anotherString.value;
//                    int i = 0;
//                    while (n-- != 0) {
//                        if (v1[i] != v2[i])
//                            return false;
//                        i++;
//                    }
//                    return true;
//                }
//            }
//            return false;
//        }

StringBuffer和Stringbuilder

The bottom layer is the variable character array value that inherits the parent class

/**
 * The value is used for character storage.
 */
char[] value;
初始化容量为16

/**
 * Constructs a string builder with no characters in it and an
 * initial capacity of 16 characters.
 */
public StringBuilder() {
    super(16);
}
这两个类的append方法都是来自父类AbstractStringBuilder的方法

public AbstractStringBuilder append(String str) {
    if (str == null)
        return appendNull();
    int len = str.length();
    ensureCapacityInternal(count + len);
    str.getChars(0, len, value, count);
    count += len;
    return this;
}
@Override
public StringBuilder append(String str) {
    super.append(str);
    return this;
}

@Override
public synchronized StringBuffer append(String str) {
    toStringCache = null;
    super.append(str);
    return this;
}

append

Stringbuffer在大部分涉及字符串修改的操作上加了synchronized关键字来保证线程安全,效率较低。

String类型在使用 + 运算符例如

String a = "a"

a = a + a;时,实际上先把a封装成stringbuilder,调用append方法后再用tostring返回,所以当大量使用字符串加法时,会大量地生成stringbuilder实例,这是十分浪费的,这种时候应该用stringbuilder来代替string。

Expansion

#注意在append方法中调用到了一个函数

ensureCapacityInternal(count + len);
该方法是计算append之后的空间是否足够,不足的话需要进行扩容

public void ensureCapacity(int minimumCapacity) {
    if (minimumCapacity > 0)
        ensureCapacityInternal(minimumCapacity);
}
private void ensureCapacityInternal(int minimumCapacity) {
    // overflow-conscious code
    if (minimumCapacity - value.length > 0) {
        value = Arrays.copyOf(value,
                newCapacity(minimumCapacity));
    }
}
如果新字符串长度大于value数组长度则进行扩容

扩容后的长度一般为原来的两倍 + 2;

假如扩容后的长度超过了jvm支持的最大数组长度MAX_ARRAY_SIZE。

考虑两种情况

如果新的字符串长度超过int最大值,则抛出异常,否则直接使用数组最大长度作为新数组的长度。

private int hugeCapacity(int minCapacity) {
    if (Integer.MAX_VALUE - minCapacity < 0) { // overflow
        throw new OutOfMemoryError();
    }
    return (minCapacity > MAX_ARRAY_SIZE)
        ? minCapacity : MAX_ARRAY_SIZE;
}

delete

这两个类型的删除操作:

都是调用父类的delete方法进行删除

public AbstractStringBuilder delete(int start, int end) {
    if (start < 0)
        throw new StringIndexOutOfBoundsException(start);
    if (end > count)
        end = count;
    if (start > end)
        throw new StringIndexOutOfBoundsException();
    int len = end - start;
    if (len > 0) {
        System.arraycopy(value, start+len, value, start, count-end);
        count -= len;
    }
    return this;
}
事实上是将剩余的字符重新拷贝到字符数组value。

Here, system.arraycopy is used to copy the array, which is faster

system.arraycopy method

转自知乎:

在主流高性能的JVM上(HotSpot VM系、IBM J9 VM系、JRockit系等等),可以认为System.arraycopy()在拷贝数组时是可靠高效的——如果发现不够高效的情况,请报告performance bug,肯定很快就会得到改进。

java.lang.System.arraycopy()方法在Java代码里声明为一个native方法。所以最naïve的实现方式就是通过JNI调用JVM里的native代码来实现。

Immutability of String

Regarding the immutability of String, here is a good answer

What is immutable?

String immutability is very simple. As shown in the figure below, assigning an existing string "abcd" to "abcedl" for the second time does not modify the data at the original memory address, but re-points to a new object, new address.
image

Why is String immutable?

Open the JDK source code, the first three lines of the java.lang.String class are written like this:

public final class String implements java.io.Serializable, Comparable<String>, CharSequence {   
  /** String本质是个char数组. 而且用final关键字修饰.*/     
private final char value[];  ...  ...
 } 

First, the String class is modified with the final keyword, which means that String cannot be inherited. Look at the following, the main member field value of the String class is a char[] array, and it is modified with final.

A field modified by final cannot be changed after it is created. Some people think that this is the end of the story, but it is not. Because although value is immutable, only the reference address of value is immutable. That doesn't stop the fact that Array arrays are mutable.

The data structure of Array is shown in the figure below.
image

That is to say, the Array variable is just a reference on the stack, and the body structure of the array is on the heap.

The value in the String class is modified with final, which just means that the reference address called value in the stack is immutable. It doesn't say that the data in the array itself is immutable. Look at the example below,

final int[] value={1,2,3} ;
int[] another={4,5,6};
 value=another;    //编译器报错,final不可变 value用final修饰,编译器不允许我把value指向堆区另一个地址。
但如果我直接对数组元素动手,分分钟搞定。

 final int[] value={1,2,3};
 value[2]=100;  //这时候数组里已经是{1,2,100}   所以String是不可变,关键是因为SUN公司的工程师。
 在后面所有String的方法里很小心的没有去动Array里的元素,没有暴露内部成员字段。

private final char value[]这一句里,private的私有访问权限的作用都比final大。而且设计师还很小心地把整个String设成final禁止继承,避免被其他人继承后破坏。所以String是不可变的关键都在底层的实现,而不是一个final。考验的是工程师构造数据类型,封装数据的功力。 

What are the benefits of immutability?

The simplest reason for this is for safety. Look at the following scenario (the commented reaction example is not clear enough, and now it is written in full), a function appendStr( ) returns after adding a "bbb" after the immutable String parameter. appendSb( ) is responsible for appending "bbb" to the mutable StringBuilder.

Summarize the immutability of the following String.

1 First of all, the final modified class only guarantees that it cannot be inherited, and the address of the object of this class in the heap memory will not be changed.

2 But the reference that holds the String object itself can be changed, for example, it can point to other objects.

3 The final modified char array ensures that the reference to the char array is immutable. But the value can be modified by char[0] = 'a'. However, String does not provide a method to complete this operation, so the immutability of String is also based on code encapsulation and access control.

for example

final class Fi {
    int a;
    final int b = 0;
    Integer s;

}
final char[]a = {'a'};
final int[]b = {1};
@Test
public void final修饰类() {
    //引用没有被final修饰,所以是可变的。
    //final只修饰了Fi类型,即Fi实例化的对象在堆中内存地址是不可变的。
    //虽然内存地址不可变,但是可以对内部的数据做改变。
    Fi f = new Fi();
    f.a = 1;
    System.out.println(f);
    f.a = 2;
    System.out.println(f);
    //改变实例中的值并不改变内存地址。


    Fi ff = f;
    //让引用指向新的Fi对象,原来的f对象由新的引用ff持有。
    //引用的指向改变也不会改变原来对象的地址
    f = new Fi();
    System.out.println(f);
    System.out.println(ff);
}
这里的对f.a的修改可以理解为char[0] = 'a'这样的操作。只改变数据值,不改变内存值。

The constant pool and intern were mentioned in the previous section.

Specific reference: https://blog.csdn.net/a724888/article/details/80041698

The next section revisits the final keyword.

Specific reference: https://blog.csdn.net/a724888/article/details/80045107

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324725913&siteId=291194637