Java String first
The basic data types that have been part of the Java language since the release of Java are byte, short, int, Long, char, float, double, Boolean. Current front we also studied the basic types of wrapper classes, know the each basic type has its packing type, JAVA is an object-oriented language, a lot of classes and methods of the parameters are required to use object sets (for example), but the basic data type is not object-oriented, this creates a lot of inconvenience, so the packaging type and automatic fuzzy packing and unpacking.
String is arguably the most used, most frequently used, and most special class in Java because it is also a literal constant, and literal constants include primitive types, String types, and null types. You can tell if a type is primitive by capitalizing the first letter of the type name. (This is because the Java library follows the camel ‘s-back naming convention. If you have any questions about enums, see Java enumerations – Enumeration Primitives.)
Because of the widespread use of Strings, Java has made many optimizations for Strings, such as thread-safety stingBuffers, fast concatenating StringBuliders, and String pools for the JVM. This has led to many interview questions about strings. Because there must be a demon, who made it special
A StringThe instructions
So let’s look at the specification of a String, and a lot of times you’re confused by the specification, but before we do that let’s look at the inheritance, because we said it’s a reference
From this inheritance we can see that it can be serialized, but also can be compared with each other, ok, next start our String journey, forest to the end of the water, then a mountain, mountain has a small mouth, as if there is light. Then he left the boat and entered through the mouth. The beginning of the extreme narrow, only the general. After a few steps, it suddenly became clear that String’s instructions were the entrance to the peach Blossom Land
/**
* The {@code String} class represents character strings. All string literals in Java programs, such as {@code "abc"}, are
* implemented as instances of this class.
* {@codeThe String} class represents a String. All string text in a Java program, such as {@code"ABC"}, are implemented as instances of this class. * Strings are constant; Their values cannot be changed after they are created. * String buffers support mutable strings. Because String objects are immutable they can be shared. For Example: * String buffers are mutable strings, because String objects are immutable, they can be shared * The class {@code String} includes methods for examining individual characters of the sequence,
* for comparing strings, for searching strings, for extracting substrings, and for creating a
* copy of a string with all characters translated to uppercase or to lowercase.
* {@codeThe String} class contains methods to check for individual characters in a sequence, These methods are mainly used to compare, search, extract substrings, and create a copy * Case mapping is based on the Unicode Standard version specified by the {@linkThe java.lang.Character} class. * case mapping is based on the Unicode standard version specified in the java.lang.Character class. * The Java language provides special support for the string concatenation operator ( + ) * The Java language provides special support for concatenating strings and converting other objects to strings. For example the String concatenation "+" operator * String concatenation is implemented through the {@code StringBuilder}(or {@code StringBuffer}) class and its {@codeAppend} method. * String concatenation is done by {@codeThe StringBuilder} or {@codeThe StringBuffer} {@codeImplemented * String conversions are implemented through the method {@code toString}, defined by {@codeObject} and nherited by all classes in Java.@codeToString}, which is defined in {@codeObject} class, For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, < I >The Java Language Specification</ I >. * For additional information on string concatenation and conversion, see The Java Language Specification, Book I've been on the Internet (http://troubleclear.com/document/2981) * < p > Unless otherwise noted, passing a <tt>null</tt> argument to a constructor or method in this class will cause a {@link* Unless otherwise specified, passing A null value to A constructor or other method throws A NullPointerException * <p>A {@code String} represents a string in the UTF-16 format
* in which <em>supplementary characters</em> are represented by <em>surrogate
* pairs</em> (see the section <a href="Character.html#unicode">Unicode
* Character Representations</a> in the {@codeCharacter} class for * more information). * String is utF-16 encoded, supplementary characters are represented by proxy pairs * Index values refer to {@code char} code units, so a supplementary character uses two positions in a {@codeString}. * specifies that the position of the subscript corresponds to a character, so the supplementary character is in {@codeString} uses two positions. * <p>The {@code String} class provides methods for dealing with Unicode code points (i.e., characters), in addition to those for dealing with Unicode code units (i.e., {@codeChar} values). *@codeChar} handles Unicode code characters outside {@codeString} also provides this method */
public final class String implements java.io.Serializable.Comparable<String>, CharSequence {... . }Copy the code
1. Main internal components of a String
The following variables value and hash are actually quite simple, but I want you to pay attention to them. When I went to an interview earlier, I was asked if I had seen the Java source code, and I answered yes. At that time that embarrassed 🙂
value
This is the real store of strings, which means strings are actually stored as character arrays
/** The value is used for character storage. */
private final char value[];
Copy the code
hash
Hash value of the string
/** Cache the hash code for the string */
private int hash; // Default to 0
Copy the code
2. Create a String
- A constructorWe know that String is not a primitive type but an object type, so we can naturally create a String object the same way we create an object type
new
Keyword, which is essentially this is the constructor of String, for exampleString STR = new String(" ABC ")
String provides a dozen constructors - literalIf you’re not sure what a literal is, that’s fine, but think about how you create underlying datatype variables, for example
int i=10
We can also create strings like this for exampleString str = “abc”;
But if you think that this approach to literals is the purpose of creating strings, you are wrong. Java provides this approach not only to simplify String creation, but also to differentiate String creation from constructor creation. What is the purpose of distinguishing? Is the String of pool behind us said, because it will distinguish between the two ways, let through literal strings of this way will go pool of this design, because by new created the String will be stored in a heap, and have their own space, through the literal String created this way will be treated differently, will be put into public String in the pool
This means that if two literals have the same content (string), they will occupy the same block of storage space in the string pool, that is, they will point to the same content
String str1 = "abc";
String str2 = "abc";
Copy the code
When you are by assigning a string literal way to create a string object, the pool will detect whether there is such a string content, if there is a reference to the already existing string variable, if not then the literal added to the string of pool and then returns to its reference, so the above two string variables share the same in string literals in the pool, There will be a brief introduction to string pools later in this article, and I will write a separate article on string pools later
2. Use of String
1. Immutability of String
Immutability is explained in the comments, so let’s look at it in detail
/**
* The {@code String} class represents character strings. All
* string literals in Java programs, such as {@code"abc"}, are * implemented as instances of this class. * <p> * Strings are constant; their values cannot be changed after they * are created. String buffers support mutable strings. * Because String objects are immutable they can be shared. For example: * ... * /
public final class String {
private final char value[];
}
Copy the code
Once a String is created in the heap, it cannot be modified. That’s because strings are placed in an array of char, which is immutable and is modified by the final keyword. One thing to note here is that the reason the String class is immutable is not because it is final, but because its underlying storage is an immutable array that cannot be modified by the program
Like this string class BuerSting, it’s mutable, and mutable means its original value has changed
public final class BuerSting {
private char value[];
private int hash; // Default to 0
public BuerSting(char value[]) {
this.value = Arrays.copyOf(value, value.length);
}
public BuerSting(String original) {
this.value = original.toCharArray();
this.hash = original.hashCode();
}
public void setValue(char[] value) {
this.value = value;
}
public void setValue(String value) {
this.value = value.toCharArray();
}
@Override
public String toString(a) {
return "BuerSting{" +
"value=" + Arrays.toString(value) +
'} ';
}
public static void main(String[] args) {
BuerSting s = new BuerSting("aaa");
System.out.println(s);
s.setValue("sss"); System.out.println(s); }}// Output the result
BuerSting{value=[a, a, a]}
BuerSting{value=[s, s, s]}
Copy the code
So we’ve seen that strings can be defined to be mutable, but why does Java make them immutable? It also has to do with string pooling, because with string pooling, you can have multiple string variables holding the same string, so if it’s mutable, Changing the contents of a string with one variable causes the contents of references to other string variables to change, so that’s why strings are designed to be immutable.
@Test
public void immutable(a) {
String str = "hello";
str.concat("world");
System.out.println("Value of str:" + str);
}
// Output the result
Value of str:hello
Copy the code
So you can see that when you modify a string using a string method, it just returns the new string that contains the modified content, the original content remains unchanged
If you think you’re done here, you’re not, because you can still do bad things by inheritance
final class
- You can break the immutability of the String type by inheriting the String class and then providing methods to modify the content
- Also, you can override hashCode and equals, which will result in the string pool repeating contents or different contents having the same hash value
Thread safety
Because strings are immutable objects, they are thread-safe
2. Define a string
public class MyString {
public static void main(String[] args) {
String str1 = "i";
String str2 = "i";
String str2 = new String("i"); System.out.println(str1 == str2); System.out.println(str1 == str3); System.out.println(str1 == str3.intern()); }}// Run the result
true
false
true
Copy the code
How to understand the above three lines of code? The first thing I need to introduce here is the concept of a string constant pool.
The string constant pool is a special separate memory space placed in the Java Heap. Before Jdk7.0, the string constant pool was stored in PermGen, which was moved to the Java Heap (still independent in the Heap), and removed PermGen in Jdk8.0. }, the Java memory model is not the focus of this chapter, see the ARTICLE on the JVM about the location of string constant pools in memory
String literals referenced by STR1 and STR2 are in the string constant pool, while objects referenced by STR3 are in the Java Heap. What, it’s not easy to understand? For example
Work one day, to go off work time, ready to see a little gold vase. After half an hour, female ticket went home, watching “Romance of The Three Kingdoms” is also what she wanted to do, I looked at the website to send her, good, she also began to read, half an hour, my father came back, he is also a fan of The Three Kingdoms, but he does not like to read online, so I bought a book in the bookstore.
The novel website mentioned above is a string constant pool, containing many literal strings, such as Romance of The Three Kingdoms, Journey to the West, a Dream of Red Mansions, etc. Each literal string keeps a unique copy in the constant pool. No matter who visits the website to read Romance of The Three Kingdoms, it has the same url and the same content.
And the ticket is the str1 and str2, we see the same web site of The Three Kingdoms, content is same, not only refer to the address is the same (string constant pool retains only “helloworld”), so the str1 = = str2 run results to true
My father is a STR3, different from me and my daughter. Although the content I read is also romance of The Three Kingdoms, the reference address is different from the physical book, and a book cannot be read by more than one person at the same time (string objects are in the Java Heap, and each new object will be created). So str1 == str3 runs false.
So let’s go back and see that a String literal always refers to the same instance of the String class because it is qualified by the string.intern () method. We can also call that method to put strings in the heap into the String constant pool, which can improve memory usage. You can also share a unique instance with the consumer.
System.out.println(str1 == str3.intern());
// The result is true
Copy the code
So what is the implementation logic of this method, let’s take a look at the source code
/** * canonical representation for the string object. * A pool of strings, initially empty, is maintained privately by the class {@codeString}. * A String pool, initially empty, * 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. The string in the string pool is returned * Otherwise, this {@code String} object is added to the pool and a reference to this {@codeString} Object is returned. * Otherwise the current String object was added to the String pool and a reference to the String is returned. * It follows that for any two strings {@code s} and {@code t}, {@code s.intern() == t.intern()} is {@code true} if and only if {@code s.equals(t)} is {@codeTrue}. * Therefore, for any two strings {@codesS} and {@code{t},@codesS.intern () ==t.intern ()} is {@codeTrue} if and only if {@codesS.quals (t)} is {@codeTrue}. * All literal strings and String-valued expressions are interned. String literals are defined in section 3.10.5 of the <cite>The Java™ Language Specification</cite>. * All literal strings and string value constant expressions are internal *@return a string that has the same contents as this string, but is
* guaranteed to be from a pool of unique strings.
*/
public native String intern(a);
Copy the code
We found that this is a native method. Looking at the notes, we found that the general process of this method is as follows:
Intern () checks if the string constant pool contains the same string literals (via equals). If so, return string literals.
2. If not, add the string object to the string constant pool and return a reference to the object in the string constant pool. The returned reference needs to be assigned, otherwise it will still refer to the address in the heap:
String str4 = new String("helloChina"); System.out.println(str4.intern() == str4); //false str4 = str4.intern(); String str5 = "helloChina"; String str6 = "helloZhonghua" System.out.println(str4 == str5); //trueCopy the code
Let’s look at the memory structure
3. Assign the value to the defined string again
str6 = "helloHuaxia";
Copy the code
“HelloHuaxia” is a String that is fixed by the final keyword. “helloHuaxia” is a String that is fixed by the final keyword
4. String processing of +
String str7 = "good good" + " study";
String str8 = "good good study";
System.out.println(str7 == str8);
Copy the code
After compiling the tool
String str7 = "good good study";
String str8 = "good good study";
Copy the code
Therefore, we can see that the compiler merges variables at compile time, instead of creating three objects in the constant pool: “good good “, “study”, and “good good study”. Str7 == str8 result true. But if that happens
String str9 = "good good ";
String str10 = str9 + "study";
system.out.println(str8 == str10);//false
Copy the code
Substring (), toUpperCase(), toUpperCase(), Trim () and so on return the address of the String in the heap.
5. Common methods of String
//str1 == "hello,world ";
// Get the length
str1.length()/ / 12;
// Intercepts a string between positions 2 and 5 (including position 2, excluding position 5, starting from 0)
str1.substring(2.5);//"llo"
// Check if there is a string "ello"
str1.contains("ello");//true via indexOf
// Get the start position of ello in str1
str1.indexOf("ello");/ / 1
// Convert a string to string data
str1.split(",");//["hello","world"]
// Remove whitespace from both sides of the string
str1.trim();//"hello,world"
Copy the code
conclusion
This article from String immutability, String literal creation and String object difference, String literal constant pool, String memory structure, commonly used String related method description, if there is any wrong, please criticize correct, hope common progress, thank you!
-
The Java language provides special support for strings
- Concatenation of strings and conversion of other objects to strings provide special support
- There is special support for creating strings through literals in order to design and use String pools
- Strings are immutable, so they are thread-safe
- The “+” operator has been overwritten to concatenate strings
- After Java7, strings can be used in switch cases
-
Java provides a string constant pool to optimize string performance
-
String is immutable because the underlying byte array is final
-
The Java Language Specification document is provided for you to check