Strings are variable types that must be supported by any programming language, some programming languages directly provide native variable types, and some programming languages use the syntax features to provide support in the form of an SDK . In the Java programming platform, support for strings is used in the latter form by providing a class named string in the JDK that corresponds to the variable type of the string.
SOURCE Analysis
Since the string class in the JDK corresponds to the type of the strings variable, in order to skillfully grasp the string-related skills in Java, we must analyze and study this class in depth. Coding industry has a famous saying called "The Source, no secret ", so our first step is to look at the source of the String class summary. The highlights are summarized below:
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... ..}
Some of the key points we have come up with are:
- The underlying string class uses an array of char to hold the data.
- The string class is a final class and is not allowed to be inherited.
- The string class is a immutable class that does not change content after the object of that class is generated. All member methods in the class that return a string type object are returned with a new string object .
JVM Memory model
Java as a half-compiled semi-explanatory or compiled and interpreted programming language, Java source files need to be compiled by the compiler into a bytecode (bytecode) file, and then on the JVM (Java Virtual machine) interpretation of execution. In order to understand and master the characteristics of the string class, it is important to know the memory model of the JVM clearly. For string types, the string class, the JVM has made specific adjustments and optimizations throughout the process from compiling the source code to executing the bytecode, and it is precisely these adjustments and optimizations that have caused some of the bizarre features of the string class and object.
For an analysis and understanding of the JVM's memory model, refer to one of my other documents or learn from other books and materials. The two knowledge points closely related to this article are summarized as follows:
- In the JVM's memory model, there is an area called the method area , which stores the loaded class information, the method body, and the various symbol tables.
- There is a section in the method area named constant , which stores the string constants at compile time and at run time.
Method anatomy
The string class acts as a class for the corresponding string, which contains a number of methods to complete the construction, cropping, splicing, and substitution of string-related functions. In particular, there are already more than 80 methods in this class when JDK8 , but most of them are overloaded methods. We should classify them according to their function as follows:
| Method Name |
function |
Method Name |
function |
| String |
Structure |
codepoint* |
Take value |
| Length |
Length |
GetChars |
Take value |
| IsEmpty |
Empty sentence |
GetBytes |
Take value |
| CharAt |
Take value |
*equals* |
Award, etc. |
| compareto* |
Comparison |
Regionmatches |
Regular |
| Startwith |
Judge |
*indexof |
Take value |
| Substring |
Intercept |
Concat |
Stitching |
| replace* |
Replace |
Matches |
Regular |
| Contains |
Contains |
Split |
Segmentation |
| Join |
Stitching |
Rol |
Transformation |
| Trim |
Go to Space |
Format |
Formatting |
| *valueof |
Transformation |
Intern |
Get |
Feature Profiling
Because of the specificity and frequency of string types, Java provides several important features when dealing with string types for functional and efficiency reasons.
equals and = =
- for = =, if variables that function with the base data type (byte, short, char, int, long, float, double, Boolean) are compared to the value of their stored values, or if the variable with reference type Compares the address of the object to which it is pointing (that is, whether the same object). In Java, string is a reference type.
- The equals method of string inherits from the Object,object of the super-parent class in Java to compare the two-object references for equality (that is, whether the same object). However, the Equals method of string is not simply inherited, but is overridden (override) to compare the value of the character sequence stored by the two string objects as equal.
How to create
There are generally 5 ways to create objects for classes in Java. They are the new keyword, the class newinstance method, the newinstance method of the constructor class, and the clone of the string object. method, and the deserialization mechanism. However, the string object also has a special way of creating it by using the " or" wrap character sequence. Now, let's focus on the similarities and differences between the new keyword and the character sequence , which are two ways to create a string object.
We learn the pros and cons of both approaches directly in code instances. First look at the code snippet one:
String stra = "www.tiantianbianma.com";
String StrB = new string ("www.tiantianbianma.com");
string strc = new string ("www.tiantianbianma.com"); System.out.println (Stra.equals (StrB));System.out.println (Stra = = StrB);System.out.println (Stra.equals (STRC));System.out.println (Stra = = STRC);System.out.println (Strb.equal (STRC));System.out.println (StrB = = STRC);
It is recommended to think about the answers and then to verify the results on the machine. The correct answer is true, False, True, False, True, false. The diagram of their memory layout in the JVM is as follows:
The answer is clear, against this figure.
Compilation optimizations
Look directly at the code snippet two:
Final String str = "ma.com";
String stra = "www.tiantianbianma.com";String StrB = "Wwww.tiantian" + "bianma.com";String strc = "Www.tiantianbian" + str;System.out.println (Stra.equals (StrB));System.out.println (Stra = = StrB);System.out.println (Stra.equals (STRC));System.out.println (Stra = = STRC);
It is recommended to think about the answers and then to verify the results on the machine. The correct answer is true, true, true, true, and hopefully not unexpected. The logic behind this is that when the Java compiler compiles the source code, the character constants that can be determined at compile time, including the sequence of characters and the final character variable, are automatically stitched and optimized.
Intern method
One of the most bizarre methods of the string class is intern, or first look at the code snippet three:
String stra = "www.tiantianbianma.com";string StrB = new string ("www.tiantianbianma.com"); System.out.println (Stra.intern (). Equals (strb.inTern ())); System.out.println (stra.intern () = = Strb.intern ());
It is recommended to think about the answers and then to verify the results on the machine. The correct answer is true, true. The rationale behind this is that whether it is a string object in a constant area of strings or a string object in heap memory, their intern method is to go back to the string object in the JVM that gets the sequence of equal characters. The JVM memory layout for the code snippet above is as follows:
The answer is clear, against this figure.
Summarize
In this paper, from the source code, and then to the analysis of methods and features, basically covering the Java string class of the key and difficult. In particular, the immutable characteristics and compilation optimization characteristics, but also in the actual project and written interview frequently encountered, there is the Intern method of the paradox. Of course, these features of string are also not perfect, and the immutable feature is a great loss of performance when a large number of strings are spliced, so you need to use the StringBuilder class or the StringBuffer class instead. In addition, this paper does not carefully analyze the specific functions and points of attention of the methods in the string class, but it needs to be mastered by the reader, whether for the written interview or the actual work is beneficial.
Disclaimer: This article is reproduced from IT Technical Analysis website: daily programming, reprinted from http://www.tiantianbianma.com/deep-insight-java-string-class/
Methods and features of in-depth analysis of Java's string classes