下面写了一段代码来展示结果
import javax.sound.midi.Soundbank;
import java.util.Arrays;
import java.util.Scanner;public class Main {public static void main(String[] args) {String str = "abc";System.out.println("str:"+str);char[] chars = str.toCharArray();System.out.println("chars:"+chars);System.out.println("chars.toString():"+chars.toString());System.out.println("Arrays.toString(chars):"+ Arrays.toString(chars));String string = new String(chars);System.out.println("string:"+string);}
}
下面是执行结果:
从结果中可以看出,如果直接调用toString()会是输出的[class name]@[hashCode]
new String()就会通过构造函数生成一个String字符串