零碎知识点
由于String对象是不可变的,所以可以共享(即两个字符串一样则共享同一块内存地址)
//共享 String t1 = "123"; String t2 = "123"; System.out.ptintln(t1 == t2);//true,(== 比较的是内存地址)
存在于方法区(加载代码的内存区),被所有的线程共享
堆在逻辑上被分为三部分:
新生代:存储刚创建的对象(GC询问次数最多为15次,>15则进入老年代)
老年代:垃圾回收次数超过15次(即15次之后依然存活的对象)
永久代:静态修饰的所有东西(类、方法、常量…)
每一个字符串对象的创建都会放入永久代(每次创建都先去永久代,前提:不用new,new开辟新内存)
String s = " ";
String str = “abc” ;
String()
String str = new String(“abc”);
区别
直接赋值:
构造方法:
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与另一个 String比较,忽略了大小写。 |
void | getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) | 将此字符串中的字符复制到目标字符数组中。 |
int | indexOf(int ch) | 返回指定字符第一次出现的字符串中的索引。 |
int | indexOf(int ch, int fromIndex) | 返回指定字符第一次出现的此字符串中的索引,从指定索引处开始搜索。 |
int | indexOf(String str) | 返回指定子字符串第一次出现的字符串中的索引。 |
int | indexOf(String str, int fromIndex) | 从指定的索引处开始,返回指定子字符串第一次出现的字符串中的索引。 |
boolean | isEmpty() | 返回 true ,当且仅当, length()是 0 。 |
int | lastIndexOf(int ch) | 返回指定字符最后一次出现的字符串中的索引。 |
int | lastIndexOf(int ch, int fromIndex) | 返回指定字符最后一次出现的字符串中的索引,从指定的索引开始向后搜索。 |
int | lastIndexOf(String str) | 返回指定子字符串最后一次出现的字符串中的索引。 |
int | lastIndexOf(String str, int fromIndex) | 返回指定子字符串最后一次出现的字符串中的索引,从指定索引开始向后搜索。 |
int | length() | 返回此字符串的长度。 |
String | repeat(int count) | 返回一个字符串,其值为此字符串的串联重复 count次。 |
String | replace(CharSequence target, CharSequence replacement) | 将此字符串中与文字目标序列匹配的每个子字符串替换为指定的文字替换序列。 |
String | replaceAll(String regex, String replacement) | 将给定替换的给定 regular expression匹配的此字符串的每个子字符串替换。 |
String | replaceFirst(String regex, String replacement) | 将给定替换的给定 regular expression匹配的此字符串的第一个子字符串替换。 |
boolean | startsWith(String prefix) | 测试此字符串是否以指定的前缀开头。 |
boolean | startsWith(String prefix, int toffset) | 测试从指定索引开始的此字符串的子字符串是否以指定的前缀开头。 |
String | stripLeading() | 返回一个字符串,其值为此字符串,并删除了所有前导 white space 。 |
String | stripTrailing() | 返回一个字符串,其值为此字符串,并删除所有尾随 white space 。 |
CharSequence | subSequence(int beginIndex, int endIndex) | 返回作为此序列的子序列的字符序列。 |
String | substring(int beginIndex) | 返回一个字符串,该字符串是此字符串的子字符串。 |
String | substring(int beginIndex, int endIndex) | 返回一个字符串,该字符串是此字符串的子字符串。 |
char[] | toCharArray() | 将此字符串转换为新的字符数组。 |
String | toLowerCase() | 使用默认语言环境的规则将此 String所有字符转换为小写。 |
String | toUpperCase() | 使用默认语言环境的规则将此 String所有字符转换为大写。 |
String | toUpperCase(Locale locale) | 使用给定 Locale的规则将此 String所有字符转换为大写。 |
static String | valueOf(T i) | 返回T参数的字符串表示形式。 |