String的getBytes()方法是得到一个系统默认的编码格式的字节数组。
import java.io.UnsupportedEncodingException;
public class NN {
public static void main(String[] args) {
String s1 = "B";
String s2 = "3";
String s30 = "z";
String s33 = "赵";
try {
System.out.println(s1.getBytes("UTF-8").length); //1
System.out.println(s1.getBytes("GBK").length); //1
System.out.println(s2.getBytes("UTF-8").length); //1
System.out.println(s2.getBytes("GBK").length); //1
System.out.println(s30.getBytes("UTF-8").length); //1
System.out.println(s30.getBytes("GBK").length); //1
System.out.println(s33.getBytes("UTF-8").length); //3
System.out.println(s33.getBytes("GBK").length); //2
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}