String
1. String 的构造方法
String s 本来是字符串常量的引用,应该打印地址,但是编译器重写了 toString 方法,所以打印 hello。
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(array, 0, 2); // 从 0 位置后拿 2 个字符
System.out.println(s4);
// 直接 new String 对象构造
String s3 = new String("hello");
System.out.println(s3);
}
}


