1. 使用 `new String(byte[] bytes)` 构造函数。
byte[] bytes = {72, 101, 108, 108, 111}; // 对应于 "Hello"
String str = new String(bytes);
System.out.println(str); // 输出: Hello
2. 使用 `String.getBytes(Charset charset)` 静态方法,并指定字符集。
byte[] bytes = "Hello".getBytes(StandardCharsets.UTF_8);
String str = new String(bytes, StandardCharsets.UTF_8);
System.out.println(str); // 输出: Hello
注意:如果不指定字符集,`new String(byte[] bytes)` 将使用平台的默认字符集进行转换,这可能导致不同平台上有不同的结果。因此,推荐使用 `StandardCharsets` 类中定义的字符集,如 `StandardCharsets.UTF_8`,来确保结果的一致性。