一、String 类深度解析
1.1 常见构建方式
在 Java 中,创建字符串的方式多种多样。这里梳理了四种最基础且常用的方法。
public static void main(String[] args) {
// 1. 使用字符串常量进行赋值
String string1 = "Hello World";
System.out.println(string1);
// 2. 使用 new 关键字
String string2 = new String("Hello World");
System.out.println(string2);
// 3. 使用字符数组构造
char[] chars = new char[]{'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'};
String string3 = new String(chars);
System.out.println(string3);
// 4. 使用字节数组构造
byte[] bytes = new byte[]{72, 101, 108, 108, 111, 32, 87, 111, , , };
(bytes);
System.out.println(string4);
}


