
The {@code String} class represents character strings.


String str=“abc”;
"abc"就是String类下的一个具体的对象



验证:


String s4="abc";
System.out.println("字符串的长度为:"+s4.length());
String s5=new String();
System.out.println("字符串是否为空:"+s5.isEmpty());
System.out.println("获取字符串的下标对应的字符为:"+s5.charAt(1));
String s6="abc";
String s7="abc";
System.out.println(s6.equals(s7));

String类实现了Comparable,里面有一个抽象方法叫comparaTo,所以String中一定要对这个方法进行重写。
String s8="abc";
String s9="abc";
System.out.println(s8.compareTo(s9));

//字符串的截取
String s10="asdfghjk";
System.out.println(s10.substring(3));
System.out.println(s10.substring(3, 6));//[3,6)
//字符串的合并/拼接
System.out.println(s10.concat("pppp"));
//字符串中的字符的替换:
String s11="aadsgwgvasasfe";
System.out.println(s11.replace('a', 'u'));
//按照指定的字符串进行分裂为数组的形式:
String s12="a-b-c-d-e-f";
String[] str2 = s12.split("-");
System.out.println(Arrays.toString(str2));
//转大小写的方法:
String s13="abc";
System.out.println(s13.toUpperCase());
System.out.println(s13.toUpperCase().toLowerCase());
//去除首位空格:
String s14=" a b c ";
System.out.println(s14.trim());
//toString()
String s15="abc";
System.out.println(s15.toString());
//转化为String类型:
System.out.println(String.valueOf(false));
public class Test02 {
public static void main(String[] args) {
String s1="a"+"b"+"c";
String s2="ab"+"c";
String s3="a"+"bc";
String s4="abc";
String s5="abc"+"";
}
}
上面的字符串会进行编译器优化,直接合并成为完整的字符串,我们可以反编译验证:
public class Test02 {
public static void main(String[] args) {
String s4="abc";
String s4="abc";
String s4="abc";
String s4="abc";
String s4="abc";
}
}
然后在常量池中,常量池的特点是第一次如果没有这个字符串,就放进去,如果有这个字符串,就直接从常量池中取:
内存:

String s6=new String("abc");
内存:
开辟两个空间(1.字符串常量池中的字符串 2.堆中开辟的空间)

public class Test03 {
public static void main(String[] args) {
String a="abc";
String b=a+"def";
System.out.println(b);
}
}
a变量在编译的时候不知道a是“abc”字符串,所以不会进行编译期优化,不会直接合并为:“abcdef”
反汇编过程:为了更好的帮我分析字节码文件时如何帮我解析的
利用IDEA中的控制台:

