1. Introduction
Scratch knowledge points
- extends Object
- implements serializable,Comparable< String >,charSequence
- The String class represents a string, and all string literals are objects of this class.
- The string is unchanged and the value cannot be changed after creation.
- Once the object is declared, it cannot be changed. All it changes is the address. The original string still exists and generates garbage.
- Any "" is a string object, and if there is no assignment, it is an anonymous object.
- Use "+" to splice strings as much as possible, generally use append+toString
- StringBuilder (thread unsafe) and StringBuffer (thread safe), ignore thread safety, everything else is the same
Since String objects are immutable,shared(i.e. the same as two stringsShare the same memory address)
//sharedString t1 = "123"; String t2 = "123"; (t1 == t2);//true,(== Comparing memory addresses)
String constant pool
Exist inMethod area(Memory area where the code is loaded) is shared by all threads
Logical division
The heap is logically divided into three parts:
New Generation: Storing newly created objects (the maximum number of GC queries is 15 times, and if >15 is entered, it will enter the old age)
Elderly: Garbage recycling times exceeds 15 times (that is, objects that still survive after 15 times)
Permanent generation: everything that is statically modified (classes, methods, constants...)
The creation of each string object will be put into a permanent generation (every time you create it, the permanent generation will be first, the premise is: no new, new opens up new memory)
2. Create an object
2.1 Direct reference to the constant area
String s = " ";
String str = “abc” ;
2.2 Using the construction method
String()
String str = new String(“abc”);
2.3 The difference between the two instantiation methods
the difference
Direct assignment:
- Only open up a pile of memory space, and it will automatically enter the pool without garbage
- Anonymous object "" will be placed into the object pool, and the anonymous object that already exists in the pool will be directly used when different objects are assigned directly.
Construction method:
- Create an object in heap memory
- Two heap memory spaces will be opened, one of which will turn into garbage and be recycled by the system, and it cannot be automatically entered into the pool. It needs to be manually entered into the pool through the public String intern(); method.
3. Common methods
Modifier and Type | Method | Description |
---|---|---|
char | charAt(int index) | Returns the char value at the specified index. |
int | compareTo(String anotherString) | Compare two strings in dictionary order. |
boolean | endsWith(String suffix) | Tests whether this string ends with the specified suffix. |
boolean | equals(Object anObject) | Compare this string to the specified object. |
boolean | equalsIgnoreCase(String anotherString) | Comparing this String with another String, ignoring case. |
void | getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) | Copy the characters from this string into the target character array. |
int | indexOf(int ch) | Returns the index in the string where the specified character first appears. |
int | indexOf(int ch, int fromIndex) | Returns the index in this string where the specified character first appears, starting with the search at the specified index. |
int | indexOf(String str) | Returns the index in the string in which the specified substring first appears. |
int | indexOf(String str, int fromIndex) | Starting from the specified index, return the index in the string in which the specified substring first appears. |
boolean | isEmpty() | Returns true if and only if length() is 0 . |
int | lastIndexOf(int ch) | Returns the index in the string where the specified character last appears. |
int | lastIndexOf(int ch, int fromIndex) | Returns the index in the string where the specified character last appears, starting from the specified index and searching backwards. |
int | lastIndexOf(String str) | Returns the index in the string that last appears in the specified substring. |
int | lastIndexOf(String str, int fromIndex) | Returns the index in the string that last appears in the specified substring, searching backwards from the specified index. |
int | length() | Returns the length of this string. |
String | repeat(int count) | Returns a string whose value is repeated counts for the concatenation of this string. |
String | replace(CharSequence target, CharSequence replacement) | Replace each substring in this string that matches the literal target sequence with the specified literal replacement sequence. |
String | replaceAll(String regex, String replacement) | Replace each substring of this string that matches the given regular expression of the given replacement. |
String | replaceFirst(String regex, String replacement) | Replace the first substring of this string that matches the given regular expression of the given replacement. |
boolean | startsWith(String prefix) | Tests whether this string begins with the specified prefix. |
boolean | startsWith(String prefix, int toffset) | Tests whether the substring of this string starting with the specified index begins with the specified prefix. |
String | stripLeading() | Returns a string whose value is this string and deletes all leading white space. |
String | stripTrailing() | Returns a string whose value is this string and removes all trailing white space. |
CharSequence | subSequence(int beginIndex, int endIndex) | Returns a sequence of characters that are subsequences of this sequence. |
String | substring(int beginIndex) | Returns a string that is a substring of this string. |
String | substring(int beginIndex, int endIndex) | Returns a string that is a substring of this string. |
char[] | toCharArray() | Convert this string to a new character array. |
String | toLowerCase() | Use the rules of the default locale to convert all characters to lowercase. |
String | toUpperCase() | Use the rules of the default locale to convert all characters to uppercase. |
String | toUpperCase(Locale locale) | Convert all characters of this String to uppercase using the rules given Locale. |
static String | valueOf(T i) | Returns the string representation of the T parameter. |
This is the end of this article about the detailed explanation of the usage of Java String classes. For more related Java String content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!