Understand all the little secrets of string in one article

brief introduction

String is a very common object type in Java. It can be said that string is the most used in Java. So what are the secrets of string? Next, this article will explain them one by one.

String is immutable

String is immutable. Officially, it is called immutable or constant.

The bottom layer of string is actually an array of char.

private final char value[];

All string literals such as "ABC" are string implementations.

Consider the following assignment operations:

String a="abc";
String b="abc";

For the Java virtual machine, "ABC" is a string literal. After JDK 7, this string literal is stored in Java heap. Before JDK 7, there was a special method area to store.

With "ABC", we assign "ABC" to a and B.

You can see that a and B here are just references to strings in Java heap.

Let's look at what happens in the following code:

String c= new String("abc");

First, create "ABC" in Java heap, then call String constructor:

    public String(String original) {
        this.value = original.value;
        this.hash = original.hash;
    }

In the constructor, string assigns the underlying string array to value.

Because the assignment of array is only the assignment of reference, the above new operation will not produce a new string literal.

But the new operation creates a new string object and assigns it to C.

The immutability of string also lies in that all operations of string will produce new string literals. The original string will never change.

The advantage of string invariance is that it is thread safe. Any thread can safely read strings.

Pass value or reference

For a long time, Java developers have such problems. Is java passing values or references?

I think this problem can be considered from two aspects.

First, for the basic types int, long and double, the assignment to them is a copy of the value. For objects, the assignment operation is a reference.

On the other hand, in the parameters of method calls, all are value passing operations.

public static void main(String[] args) {
	String x = new String("ab");
	change(x);
	System.out.println(x);
}
 
public static void change(String x) {
	x = "cd";
}

Let's look at the above example, which outputs ab. Because x is a reference to "ab", but in the change method, because it is a value passing call, a new x will be created, and its value is the reference address of "ab". When x is reassigned, only the value of X after copying is changed. The value of X itself is constant.

Memory leak caused by substring()

The first time you see this topic, you may be surprised that substring method can lead to memory leakage? This topic starts with JDK 6.

Let's first look at the implementation of JDK 6:

String(int offset,int count,char value[]) {
	this.value = value;
	this.offset = offset;
	this.count = count;
}
 
public String substring(int beginIndex,int endIndex) {
	//check boundary
	return  new String(offset + beginIndex,endIndex - beginIndex,value);
}

As you can see, the substring method of JDK 6 still refers to the original string array. The only difference is that offset and count are different.

Let's consider the following applications:

String string = "abcdef";
String subString = string.substring(1,3);
string = null;

Although we finally assigned string null, substring still refers to the original string. Will not be garbage collected.

After JDK 7, the implementation of string sends the following changes:

public String(char value[],int offset,int count) {
	//check boundary
	this.value = Arrays.copyOfRange(value,offset,offset + count);
}
 
public String substring(int beginIndex,int endIndex) {
	//check boundary
	int subLen = endIndex - beginIndex;
	return new String(value,beginIndex,subLen);
}

Arrays. Copyofrange will copy a new array instead of using the previous array. Thus, the above memory leakage problem will not occur.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>