java中getByte()到底是用来干什么用的?

如题所述

getBytespublic byte[] getBytes()使用平台的默认字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
当此字符串不能使用默认的字符集编码时,此方法的行为没有指定。如果需要对编码过程进行更多控制,则应该使用 CharsetEncoder 类。
返回:所得 byte 数组
从以下版本开始:JDK1.1

1、getByte():实现将字符串转化为字节数组
例如:
String myString="abcd";
byte myByte[]=myString.getBytes();
System.out.println("myByte[1]="+myByte[1]);
System.out.println("myByte[2]="+myByte[2]);
System.out.println("1.myByte[1]+myByte[2]="+myByte[1]+myByte[2]);
System.out.println("2.myByte[1]+myByte[2]="+(myByte[1]+myByte[2]));
输出结果:
myByte[1]=98
myByte[2]=99
1.myByte[1]+myByte[2]=9899
2.myByte[1]+myByte[2]=197
温馨提示:答案为网友推荐,仅供参考