String
1. String 的构造方法
字符串常量引用通常打印内容而非地址,因为编译器重写了 toString 方法。
public class Test {
public static void main(String[] args) {
// 用常量字符串构造
String s1 = "hello";
System.out.println(s1);
// 用字符数组构造
char[] array = new char[]{'a', 'b', 'c'};
String s2 = new String(array);
System.out.println(s2);
char[] array1 = new char[]{'a', 'b', 'c'};
String s4 = new String(array1, 0, 2); // 从 0 位置后拿 2 个字符
System.out.println(s4);
// 直接 new String 对象构造
String s3 = new String("hello");
System.out.println(s3);
}
}


