只记: String.valueOf()底层永远都是toString()
- 基本数据类型变成String: String.valueOf(基本数据类型)
public static String valueOf(int i) {
return Integer.toString(i);//底层还是toString()
}
- 将数组变为String: String arr = new String(数组)
- 引用类型变成String: (Object)引用.toString() [跳过了String.valueOf()直接toString了]
String底层是一个常量数组,所以String创建后不能更改:
JDK8中:
注意:JDK9开始不再是char的数组了,改为byte[ ] + coder(表示编码方式)
只要存在一个字符大于一个字节(char > 0xFF(255)),那么将会把byte数组的长度改为两倍char数组的长度,用两个字节存放一个char
存储String:"ABCDE"真实情况:【String类、数组都是 final】
面试题:为什么String是不可变的?
答:String源代码中,字符串实际是用byte数组存储的,而这个数组的引用有final修饰,所以这个引用指向byte数组后不能再改变。而数组一旦创建长度不可变。所以String创建后是不能改变的。
常量池
只有这两种方法才能成为常量池对象:
1. 以" “形式在代码中赋值的对象;
2. 多个” "简单+而成的String对象。
e、a都不会存入常量池:(但"de",“a”,“b”,“c”,"d"会成为常量池对象)
String e = b + "de";
String a = String.join("a","b","c","d");
所以,可以直接写: “哇哈哈”.equals(“哈哈”);(其中 "哇哈哈"和"哈哈"都会被JVM在方法区字符串常量中创建为对象!!,所以"哇哈哈"可以调用方法)
2. 常量池中的变量值是不会重复的,会重复利用
===>常量池的意义:String使用太频繁,让所有String值都最终存储在常量池中,并保证其不会重复,以防止内存浪费。
String不可变是指字符串常量池里的对象不可变:
注:Java中万物皆对象(除基本类型外),且对象皆继承Object老父亲!
而基本类型没有继承Object:
String中常用方法
其中构造方法中的String(byte bytes[])
注意:byte[]数值会赋予底层的char[],而char又会当做字符解码!!! JDK8中:
常用构造方法
1. String s = "abc";//直接在常量池里创建对象
2. String s = new String("");
3. String s = new String(char数组);
4. String s = new String(char数组,起始下标,长度);
5. String s = new String(byte数组);
6. String s = new String(byte数组,起始下标,长度);
常用普通方法
永远是左闭右开!
-
Returns the char value at the specified index:char charAt(int index)
-
Compares two strings lexicographically比较字符串在字典中大小:int compareTo()
-
Returns true if and only if this string contains the specified sequence of char values包含否?:boolean contains(CharSequence s)(CharSequance实际是String的一个接口,也就是String)
-
Tests if this string ends with the specified suffix以子字符串为后缀:boolean endsWith(String suffix)
-
Tests if this string starts with the specified prefix以子字符串为前缀.:boolean startsWith(String prefix)
-
Compares this string to the specified object:boolean equals(Object anObject)
-
Compares this String to another String, ignoring case considerations.忽略大小写:boolean equalsIgnoreCase(String anotherString)
-
Encodes this String into a sequence of bytes using the platform’s default charset, storing the result into a new byte array.转为byte[](与String(byte bytes[]正好相反):byte[] getBytes()
-
Converts this string to a new character array.:char[] toCharArray()
-
返回第一次出现的位置:(fromIndex:代表指定开始索引位置)
int indexOf(String/char s)
int indexOf(String/char s, int fromIndex)
返回最后一次出现的位置:
int lastIndexOf(String/char s)
int lastIndexOf(String/char s, int fromIndex) -
Returns true if, and only if, length() is 0.:boolean isEmpty(),注意不是判断null!!若null调用则会NullPointerException
-
Returns the length of this string.:int length() 【数组length是属性,String则是方法】
-
出现的全部替换:String replace(String/char old, String/char new),其实源代码中是CharSequence和char,而CharSequence是String的一个接口,其实写String一样。
-
Replaces substring of this string that matches the given regular expression with the given replacement.:regex代表正则表达式
替换所有符合正则表达式:String replaceAll(String regex, String replacement)
替换第一个符合正则表达式:String replaceFirst(String regex, String replacement) -
Tells whether or not this string matches the given regular expression.:判断是否符合正则表达式:boolean matches(String regex)
-
Splits this string around matches of the given regular expression.:
重要:String[] split(String regex)
String[] split(String regex, int limit)【limit表示分割次数,若为-1表示尽可能多得分割】
-
Returns a string that is a substring of this string.截取一个子字符串:String substring(int beginIndex)
String substring(int beginIndex, int endIndex) -
Converts all of the characters in this String to lower case using the rules of the default locale.:String toLowerCase()
-
全部转换为大写:String toUpperCase()
-
Returns a string whose value is this string, with any leading and trailing whitespace removed.删除前后空白格:String trim()
只有一个静态方法 static String valueOf(any)
将任何非字符串转换为字符串:static String valueOf(基本数据类型、char[]、布尔、Object)
注意:Object的引用会被调用toString()方法返回
解释print()
print调用String.valueOf(any)方法,全部以字符串形式输出!
所以,print(引用) 就会调用valueOf()方法,接着valueOf()调用toString()方法:
java.lang.StringBuffer
StringBuffer:可变长度的字符串
JDK9以上
原因:String底层存储:
StringBuffer底层存储:
用途:字符串经常拼接:
每次扩容二倍+2
StringBuilder常用在toString()方法中:
注意append方法特点:
- append(引用) 后还是:String.valueOf(obj) ====> obj.toString();
- append后返回append后的StringBuilder!
java.lang.StringBuffer与java.lang.StringBuilder区别
两者用法一样
区别:
面试题: