Explanation of Java String Class Usage
- 2021-10-11 18:41:45
- OfStack
1. Introduction
Pieces of knowledge
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 immutable and the value cannot be changed after it is created Object 1 cannot be changed once declared, only the address is changed, the original string still exists, and garbage is generated Any 1 "" is a string object, and if there is no assignment, it is an anonymous object Try to avoid splicing strings with "+", and use append+toString as a whole StringBuilder (thread unsafe) and StringBuffer (thread safe), ignoring thread safety, others are all the sameBecause String objects are immutable, they can be shared (that is, two strings of 1 share the same block of memory address)
// Sharing
String t1 = "123";
String t2 = "123";
System.out.ptintln(t1 == t2);//true,(== The memory address is compared )
String constant pool
Exists in the method area (the memory area where the code is loaded) and is shared by all threads
Heap is divided logically
The heap is logically divided into three parts:
Cenozoic: Store newly created objects (GC queries up to 15 times, > 15 will enter the old age)
Old age: The number of garbage collections exceeds 15 times (that is, objects that still survive after 15 times)
Perpetual generation: Everything statically decorated (classes, methods, constants, etc.)
Every 1 string object will be created into the permanent generation (every time you create it, you will go to the permanent generation first, if you don't use new and new to open up new memory)
2. Create an object
2.1 Direct reference constant range
String s = " ";
String str = " abc " ;
2.2 Using constructors
String()
String str = new String( " abc " );
2.3 Differences between two instantiation methods
Difference
Direct assignment:
Only 1 block of heap memory space will be opened, and it will be automatically put into the pool without generating garbage The anonymous object "" will be put into the object pool, and the existing anonymous objects in the pool will be directly used when different objects are assigned directly in the next timeConstruction method:
Creating Objects in Heap Memory Two pieces of heap memory space will be opened up, of which one piece of heap memory will become garbage and be recycled by the system, and it cannot be automatically put into the pool. It needs to pass public String intern (); Method to enter the pool manually.3. Common methods
Modifier and Type | Method | Description |
---|---|---|
char | charAt(int index) | 返回指定索引处的 char值。 |
int | compareTo(String anotherString) | 按字典顺序比较两个字符串。 |
boolean | endsWith(String suffix) | 测试此字符串是否以指定的后缀结尾。 |
boolean | equals(Object anObject) | 将此字符串与指定的对象进行比较。 |
boolean | equalsIgnoreCase(String anotherString) | 将此 String与另1个 String比较,忽略了大小写。 |
void | getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) | 将此字符串中的字符复制到目标字符数组中。 |
int | indexOf(int ch) | 返回指定字符第1次出现的字符串中的索引。 |
int | indexOf(int ch, int fromIndex) | 返回指定字符第1次出现的此字符串中的索引,从指定索引处开始搜索。 |
int | indexOf(String str) | 返回指定子字符串第1次出现的字符串中的索引。 |
int | indexOf(String str, int fromIndex) | 从指定的索引处开始,返回指定子字符串第1次出现的字符串中的索引。 |
boolean | isEmpty() | 返回 true ,当且仅当, length()是 0 。 |
int | lastIndexOf(int ch) | 返回指定字符最后1次出现的字符串中的索引。 |
int | lastIndexOf(int ch, int fromIndex) | 返回指定字符最后1次出现的字符串中的索引,从指定的索引开始向后搜索。 |
int | lastIndexOf(String str) | 返回指定子字符串最后1次出现的字符串中的索引。 |
int | lastIndexOf(String str, int fromIndex) | 返回指定子字符串最后1次出现的字符串中的索引,从指定索引开始向后搜索。 |
int | length() | 返回此字符串的长度。 |
String | repeat(int count) | 返回1个字符串,其值为此字符串的串联重复 count次。 |
String | replace(CharSequence target, CharSequence replacement) | 将此字符串中与文字目标序列匹配的每个子字符串替换为指定的文字替换序列。 |
String | replaceAll(String regex, String replacement) | 将给定替换的给定 regular expression匹配的此字符串的每个子字符串替换。 |
String | replaceFirst(String regex, String replacement) | 将给定替换的给定 regular expression匹配的此字符串的第1个子字符串替换。 |
boolean | startsWith(String prefix) | 测试此字符串是否以指定的前缀开头。 |
boolean | startsWith(String prefix, int toffset) | 测试从指定索引开始的此字符串的子字符串是否以指定的前缀开头。 |
String | stripLeading() | 返回1个字符串,其值为此字符串,并删除了所有前导 white space 。 |
String | stripTrailing() | 返回1个字符串,其值为此字符串,并删除所有尾随 white space 。 |
CharSequence | subSequence(int beginIndex, int endIndex) | 返回作为此序列的子序列的字符序列。 |
String | substring(int beginIndex) | 返回1个字符串,该字符串是此字符串的子字符串。 |
String | substring(int beginIndex, int endIndex) | 返回1个字符串,该字符串是此字符串的子字符串。 |
char[] | toCharArray() | 将此字符串转换为新的字符数组。 |
String | toLowerCase() | 使用默认语言环境的规则将此 String所有字符转换为小写。 |
String | toUpperCase() | 使用默认语言环境的规则将此 String所有字符转换为大写。 |
String | toUpperCase(Locale locale) | 使用给定 Locale的规则将此 String所有字符转换为大写。 |
static String | valueOf(T i) | 返回T参数的字符串表示形式。 |