这是我们的字符串。
String str = "亚洲是大陆!";
现在,让我们使用字节数组和getBytes()方法来实现我们的目的。
byte[] byteVal = str.getBytes();
现在,如果我们要获取数组的长度,它将返回该长度,如下面完整的示例所示:
public class Demo {
public static void main(String args[]) {
String str = "亚洲是大陆!";
System.out.println(str);
//转换为字节数组
byte[] byteVal = str.getBytes();
//得到长度
System.out.println(byteVal.length);
}
}输出结果
亚洲是大陆! 20