string objects in-depth understanding of java

Here to do a little in-depth understanding of the Java String object.

Evolution Java object implementation

String objects is one of the most frequently used objects in Java, so Java developers are constantly achieving String object optimized to enhance the performance of String objects.

Java6 property as well as previous versions of the String object

In previous versions and Java6, String char array objects is encapsulated object implementation, there are four members which member variables are char array, offset offset, count the number of characters in the hash value and the hash. String object is to locate the char [] array by offset and count two attributes, get the string. Doing so efficiently and quickly share an array of objects, while saving memory space, but this way it may lead to memory leaks.

Java7,8 version String object's properties

Java7 version from the beginning, Java String class to make some changes, specifically the String class is no longer offset and count two variables. The benefit of this is the String object memory occupied by a little lacking, but String.substring () method is no longer shared char [], and thus solve the memory leak problem that may result from the use of this method.

Properties Java9 and later versions of the String object

From the beginning Java9 version, Java will char [] array instead of byte [] array. As we all know, char is two bytes, if the case used to store a byte of memory space can be wasted. In order to save this byte space, Java developers have changed a byte used to store a byte string.

Further, in Java9, String object maintains a Coder new property, this property is the encoding format identifier, when calculating the length of the string or calls the indexOf () method, this field will need to determine how to calculate the length of the string . Default attributes coder has two values ​​0 and 1, where 0 represents Latin-1 (single-byte code), a represents a UTF-16 encoding.

String way to create objects and stored in memory

In Java, the basic data types of variables and references to objects stored in the local variable table stack memory in; and through new keywords and target Constructor created, it is stored in the heap memory. String object created embodiment generally two types literal (constant string) of ways, one way is to Constructor (String ()), and stored in memory in two ways different.

Literal (string constant) to create a way

When you create a string using a literal way, JVM will first check whether the literal string constant presence in the pool, if there is, in the literal referenced memory address is returned; if not, then the string constant the literal pool creation and returns a reference. The benefits created using this approach is to avoid the same string value is repeatedly created in memory, saving memory, but such an approach will be relatively easy to read number.

String str = "i like yanggb.";

String constant pool

Here we must explain the constant pool. JVM is a constant pool to reduce repetitive string object is created, in particular, maintains a special memory, this memory pool is called a string constant or string literal pool. In JDK1.6 and earlier, the runtime constant pool is in the method area. In JDK1.7 and later versions of the JVM, the runtime constant pool has been shifted out of the zone method, has opened up an area used to store runtime constant pool in the Java heap (Heap). From JDK1.8 start, JVM Java method area canceled, replaced by a direct memory element located in the space (MetaSpace). Conclusion is that the current string constant pool in the heap.

It features we know a few String objects are derived from the String constant pool.

1. In the constant pool will be shared by all of String objects, String object is not modified, because once is modified, it will cause all of this String object reference variables are subsequently changed (changing references), so the String object is It is designed to be non-modifiable, behind this immutable characteristic will make an in-depth understanding.

Poor performance of string concatenation statement 2.String objects also comes from this, because String objects immutable characteristics, each modification (here splicing) is to return a new string object rather than the original again string objects make modifications, thus creating a new string object consumes more performance (open up additional memory space).

3. Because String objects created constant pool is shared, so use double quotes statement String object (literal) will be directly stored in the constant pool, if the literal before already exists, will direct reference already exists String object, which has been described above, too, mentioned again here, particularly in order to illustrate this approach ensures that each String object constant pool is unique, also reached the purpose of saving memory.

Constructor (String ()) to create a way

When you create a string use the constructor, JVM will also check whether the literal string constant presence in the pool, but after checking the situation and create a way to use literal differently. If there is, it will create an additional heap String object, then the literal references within this String object, and returns the String object reference in the memory address; if not, will be the first in a string constant pool created in the literal, and then create a String object in the heap, and then refer to the literal inside the String object, and returns the String object.

String str = new String("i like yanggb.");

This means that as long as this way, the constructor will further open up space in the heap, create a new String object. Specifically appreciated that, in the case where there is no corresponding literal string constant pool, new String () creates two objects, placed in a constant pool (literal), into a heap memory (character string object).

Comparison of String objects

String comparison whether two objects are equal, there is usually == [] and [equals ()] two methods.

In the basic data types, you can only use [==], that is, compare their values ​​are the same; and for objects (including String), the comparison is [] == address is the same as [equals ()] was is to compare their contents are the same; and equals () is a function Object has in itself requires the internal values.

String str = "i like yanggb.";
String str1 = new String("i like yanggb.");

System.out.println(str == str1); // false
System.out.println(str.equals(str1)); // true

Because String objects created using literal way to create a String object using the constructor and the way memory address is different, but the contents is the same, it led to the above results.

intern String object () method

As we all know, String objects there are many practical ways. Why other methods do not say, here to explain this particular intern () method does, because of the intern () method is the most special. Its particularity is that this method is almost irrelevant in the business scenario, it is there that is difficult programmers, it can be said is designed to help programmers understand the JVM memory structures exist (? I believe you ghost you bad old man very bad).

/**
* When the intern method is invoked, if the pool already contains a
* string equal to this {@code String} object as determined by
* the {@link #equals(Object)} method, then the string from the pool is
* returned. Otherwise, this {@code String} object is added to the
* pool and a reference to this {@code String} object is returned.
**/
public native String intern();

The above is the source of the intern () method described in the official Note, probably means intern () method is used to return a string constant pool, if the string constant pool already exists, the object is returned directly constant pool references. Otherwise, the object is added to the constant pool, and then returns a reference. Then we can see from the intern method signature () method is a native method.

Below to learn more about the usage () method of the intern few examples.

The first example

String str1 = new String("1");
System.out.println(str1 == str1.intern()); // false
System.out.println(str1 == "1"); // false

In the above example, Intern () method returns a reference to a constant pool, and is stored str1 reference objects in a heap, so the results of the two print statements are false.

The second example

String str2 = new String("2") + new String("3");
System.out.println(str2 == str2.intern()); // true
System.out.println(str2 == "23"); // true

In the above example, the preservation of the heap str2 is a String object reference, and this JVM optimized [+] concerned. In fact, in the first statement to str2 assignment, create three objects are created in the string constant pool 2 and 3, as well as in a String object heap created 23. Because there is no string object string constant pool 23, so here pay special attention to: intern () method takes a completely different treatment options when the string object will be added to existing heap constant pool - not in establish literal constant pool, but directly references the string object itself is copied to the constant pool, that is, save the constant pool is a reference to a string object heap that already exists. According to the previous statement, this time calling intern () method will copy a string constant reference to the existing heap in the string constant pool, and then return to the string constant pool that already exists on the heap references quoted string constant pool (that is around, you bite me ah). Thus, after the end of the call intern () method returns the result of the heap is the String object reference, this time using [] == to compare, the result returned is true. Similarly, the literal constant pool 23 is not the true sense of the literal 23, and its true identity is that String objects in the heap 23. In this case, use [] == to compare literals 23 and str2, the result is true.

A third example

String str4 = "45";
String str3 = new String("4") + new String("5");
System.out.println(str3 == str3.intern()); // false
System.out.println(str3 == "45"); // false

Suddenly this example looks like more complex than the previous example, but in fact the first example above is the same, but the hardest to understand is the second example.

So here is not to say, as for why should we give this example, I believe that smart you suddenly understand (I have health insurance, you beat me to it).

Immutability of String objects

Look at a source String object.

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;
}

From the class signature look, with the String class final qualifier, which means that this class can not be inherited, this is the first decision point String objects immutable characteristics. From an array of char class [] value point of view, the class member variables are private and final modifier modified, which means that its value once initialized can no longer be changed, it was decided String objects immutable characteristic Second point.

Java Developers why should String object as immutable, mainly be to consider the following three aspects:

1. Security. Assume String object is variable, then the String object can be maliciously modified.

2. unique. This approach ensures that hash property values do not change frequently, it ensures the uniqueness that is similar to HashMap container in order to achieve the appropriate key-value cache function.

3. functionality. Can achieve a string constant pool (Which came first design, it would first have to achieve it).

Optimization of the String object

Java is one of the commonly used string type, so the operation of the string is unavoidable. In the process of the string of operation, if used properly, the performance may be bad days to do, so there are some places to take note of.

Performance optimization string concatenation

Is a string concatenation operator of strings used most frequently. As we all know the immutability of String objects, so we have to minimize during development using [+] string splicing operation. This is because the use [+] to string concatenation, will have a lot of useless objects in front to get the final results you want.

String str = 'i';
str = str + ' ';
str = str + 'like';
str = str + ' ';
str = str + 'yanggb';
str = str + '.';

System.out.println(str); // i like yanggb.

In fact, if we are using an IDE to write code relatively intelligent, the compiler will be prompted to optimize the code to use StringBuilder or StringBuffer object to optimize the performance of a string of stitching, because StringBuilder and StringBuffer is a variable object, avoiding the wasteful process of the objects. The difference between these two alternatives is in need thread-safe situation, the choice of StringBuffer object, which is supported by thread-safe; and without the need for thread-safe situation, the choice of StringBuilder object, because the performance of StringBuilder object in In this scenario, than StringBuffer object or a String object is much better.

Using intern () method to optimize memory usage

Tucao front of the intern () method of little use in actual development, but here is the use intern () method to optimize memory usage, this man really is, hey, really fragrant. About the use of the method is not to say, there are detailed instructions above, where it said a good specific application scenarios. One Twitter engineers shared a String object to optimize their cases in Qcon global software development conference, they took advantage of this String.intern () method will require 20G of memory storage optimization ago to require only a few megabytes of memory. Concrete is using intern () method would otherwise need to create a String object heap memory are placed in the constant pool, not because of the repetitive nature of the constant pool (exists returns a reference), thus avoiding a large number of duplicate String objects cause memory waste.

What, you want me to intern () method to apologize? impossible. String.intern () method is good, but also requires a combination of scenarios to use, it can not be used indiscriminately. Because in fact, achieve constant pool is similar to a HashTable implementation, and the larger the data stored in the HashTable, traversing the time complexity will increase. This means that, if the data is too large, then the burden of the entire string constant pool will be greatly increased, it is possible to enhance the performance will not get it but declined.

String split performance optimization

Dividing the string is one of the common operating string operations, for dividing the character string, most people use are split () method, split () method is received in most scenarios are regular expression parameter, this division method itself is not a problem, but because of the regular expression performance is very unstable, inappropriate use of words can cause back problems and lead to occupy high CPU. split () method is not used in the following two cases regular expression:

1. The length of the parameter passed to 1, does not contain "$ |.? () [{^ * + \" Regex metacharacters case, do not use regular expressions.

2. The length of the parameter passed 2, the first character is a backslash, and the case where the second character is not numeric or ASCII ASCII letters, not a regular expression.

So when we split the string should be used cautiously split () method, but first consider using String.indexOf () method to a string split in String.indexOf () can not meet the requirements of time and then split using the Split () method . And when using the split () string method split, need extra attention back problems.

to sum up

Although also can use a String object without knowing the String object to develop, but can help us understand the String object to write better code.

 

"Only last hope in the story, I'm still me, you are still you."

Guess you like

Origin www.cnblogs.com/yanggb/p/11613042.html