package basictype;
/**
* byte、char和String类型相互转换
*/
public class CHJavaType {
public static void main(String[] args) {
String string = "abcd";
// String转char[]
char[] chars = string.toCharArray();
for (char c : chars) {
System.out.println(c);
}
// char[]转String
System.out.println("String.valueOf:" + String.valueOf(chars));
System.out.println("new String:" + new String(chars));
byte[] bytes = {};
try {
// String转byte[]
bytes = string.getBytes("UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
// byte[]转String
System.out.println(new String(bytes));
}
}