String class
-
String is declared final and cannot be inherited
-
String implements the Serializable interface, indicating that the string is serializable
-
String implements the Compareble interface, indicating that String can be compared in size.
-
String internally defines final char[] value value for storing string data, indicating the immutable character characteristics---- 1. When reassigning a string, the specified memory area assignment needs to be rewritten, and the original value cannot be used The value is assigned. 2. When concatenating existing strings, you also need to re-specify the memory area assignment. 3. When calling the replace method of String to modify the specified character or string, you also need to re-specify the memory area assignment.
-
To assign a value to a string using a literal (different from new), the string value is declared in the string constant pool. Strings with the same content will not be stored in the constant pool.
package StringTT;
import org.junit.Test;
public class Stringlei {
@Test public void test1(){ String s1 = "abc"; // 字面量的定义方式 String s2 = "abc"; System.out.println(s1 == s2); // 对象比较的是地址值 s1 = "hello"; System.out.println(s1); System.out.println(s2); System.out.println("*****************"); String s3 = "abc"; s3 += "def" ; System.out.println(s3); }
}
Create String object
new is created in the heap space
String s = new String("abc"); method to create objects, how many objects are created in memory? Two: one is the new structure of the heap space, and the other is the data in the char[] object constant pool: "abc"
package StringTT;
import org.junit.Test;
public class StringTest2 {
@Test
public void test(){
//方式一:通过字面量定义
String s1 = "javaEE";
String s2 = "javaEE";
// 方式二:用new 在堆空间中创建
String s3 = new String("javaEE");
String s4 = new String("javaEE");
System.out.println(s1 == s2); // true
System.out.println(s3 == s4); // false
}
}
The splicing result of constants and constants is in the constant word, and the splicing of constants and variables will result in the heap
@Test
public void test1(){
String s1 = "javaEE";
String s2 = "hadoop";
String s3 = "javaEEhadoop";
String s4 = "javaEE" + "hadoop";
String s5 = s1 + "hadoop";
String s6 = "javaEE" + s2;
String s7 = s1 + s2;
System.out.println(s3 == s4); // true
System.out.println(s3 == s5); // false
System.out.println(s3 == s6); // false
System.out.println(s3 == s7); // false
System.out.println(s5 == s4); // fasle
System.out.println(s5 == s6); // fasle
System.out.println(s5 == s7); // fasle
String s8 = s5.intern(); // 得到的是在常量池
System.out.println(s8 == s3); // true
}
String method
Conversion between String and char[]
-
String to char[] using String.toCharArray() method
-
Convert char[] to String and call the constructor of String
package StringTT;
import org.junit.Test;
public class StringTest1 {
// String 和 byte的转换
@Test
public void test123(){
String str1 = “abc123”;
char[] charArray = str1.toCharArray();
for (int i = 0;i < 100; i++){
System.out.println(charArray[i]);
}
}@Test public void test2(){ char[] ch1 = new char[]{'h','e','l','l','o'}; String str2 = new String(ch1); System.out.println(str2); }
}
Conversion between String and byte[]
If you have Chinese, you can't convert a number.
Encoding: string --> byte (understandable --> unintelligible) String to byte[] Use String's getBytes() method to
decode: byte --> string ( Unintelligible binary -> understandable) byte[] -->
During the String decoding process, it must be consistent with the character set used for encoding
package StringTT;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import org.junit.Test;
public class StringTest1 {
// String 和 byte的转换
@Test
public void test123(){
String str1 = "abc123";
char[] charArray = str1.toCharArray();
for (int i = 0;i < str1.length(); i++){
System.out.println(charArray[i]);
}
}
@Test
public void test2(){
char[] ch1 = new char[]{'h','e','l','l','o'};
String str2 = new String(ch1);
System.out.println(str2);
}
@Test
public void test3() throws UnsupportedEncodingException{
String str1 = "abc123中国";
byte[] b1 = str1.getBytes(); // 使用默认的字符集进行转换
System.out.println(Arrays.toString(b1)); //输出
byte[] b2 = str1.getBytes("gbk"); //使用gbk字符集进行编码
System.out.println(Arrays.toString(b2));
//解码 byte[] --> String
String s1 = new String(b1); // 默认的字符集进行解码
System.out.println(s1);
}
}
Similarities and differences between StringBuffer, StringBuilder and String
- String is an immutable character sequence, and the bottom layer is stored using char[]
- StringBuffer is a variable sequence of characters, thread-safe and inefficient. The bottom layer uses char[] for storage.
- StringBuilder variable character sequence, new in JDK5.0, thread-unsafe, high efficiency, the bottom layer uses char[] for storage
Source code analysis
String str = new String();//char[] value = new char[0];
String str = new String(“abc”); // char[] value = new char[]{‘a’,‘b’,‘c’};
StringBuffer str = new StringBuffer();//char[] value = new char[16]; The underlying layer creates a character array with a length of 16
StringBuffer str1 = new StringBuffer("abc"); // char[] value = new char("abc".length + 16) creates a character array of length 19
Question 1: What is the length of printed str1? Or 3
questions 2: If the underlying data cannot hold the data to be added, then the underlying array needs to be expanded. By default, the expansion is twice the original capacity + 2, and the elements of the original array are copied to the new array at the same time
Guiding significance: It is recommended to use StringBuffer(int capacity) or StringBuilder(int capacity) during development, depending on whether security needs to be considered
package StringTT;
import org.junit.Test;
public class StringBufferBuiderTest {
@Test
public void test(){
StringBuffer sb1 = new StringBuffer("abc");
sb1.setCharAt(0, 'm');
System.out.println(sb1);
String s1 = "abc";
s1.replace('a','m');
System.out.println(s1); //s1不变
}
}
public String substring – Returns a substring of a left-closed right-open interval starting from end and ending at index
Arranged from high to low efficiency: StringBuilder > StringBuffer > String
Date and time API before JDK8.0
@Test
public void test(){
long time = System.currentTimeMillis();
System.out.println(time); //1970/1/1 到我们现在的时间差 毫秒数,通常叫一个时间戳
}