Java小白踩坑录 - new String 乱码(二)

简介: Java小白踩坑录 - new String 乱码(二)

推测可能是编码问题,深入其源码内部,看看:

 /**
 * Constructs a new {@code String} by decoding the specified array of bytes
 * using the platform's default charset. The length of the new {@code
 * String} is a function of the charset, and hence may not be equal to the
 * length of the byte array.
 *
 * <p> The behavior of this constructor when the given bytes are not valid
 * in the default charset is unspecified. The {@link
 * java.nio.charset.CharsetDecoder} class should be used when more control
 * over the decoding process is required.
 *
 * @param bytes
 * The bytes to be decoded into characters
 *
 * @since JDK1.1
 */
 public String(byte bytes[]) {
  this(bytes, 0, bytes.length);
 }


翻译过来就是:在通过解码使用平台 缺省字符集 的指定 byte 数组来构造一个新的 String 时,该新 String 的长度是字符集的一个函数,因此,它可能不等于 byte 数组的长度。当给定的所有字节在缺省字符集中并非全部有效时,这个构造器的行为是不确定的。


罪魁祸首就是 String(byte[]) 构造。



问题解决

小白承认了自己的错误,小T也高兴得提了个 Bug。接下来小白就要修改掉这个 Bug 了。



public static void main(String[] args) throws UnsupportedEncodingException {
  byte bytes[] = new byte[256];
  for (int i = 0; i < 256; i++)
    bytes[i] = (byte)i;
  String str = new String(bytes,"ISO-8859-1");
    for (int i = 0, n = str.length(); i < n; i++)
    System.out.print((int)str.charAt(i) + " ");
}

指定字符集后,小T和小白又能愉快得玩耍了。

 

总结

每当你要将一个 byte 序列转换成一个String 时,你都在使用某一个字符集,不管你是否显式地指定了它。如果你想让你的程序的行为是可预知的,那么就请你在每次使用字符集时都明确地指定。    

目录
相关文章
|
20小时前
|
Java 编译器 ice
【Java开发指南 | 第十五篇】Java Character 类、String 类
【Java开发指南 | 第十五篇】Java Character 类、String 类
8 1
|
4天前
|
安全 Java 编译器
Java中String、StringBuilder和StringBuffer的区别
Java中String、StringBuilder和StringBuffer的区别
|
7天前
|
存储 缓存 安全
【 Java中String源码分析(JVM视角你不来看看?】
【 Java中String源码分析(JVM视角你不来看看?】
13 0
|
13天前
|
Java
Java String类型转换成Date日期类型
Java String类型转换成Date日期类型
|
13天前
|
存储 Java 对象存储
String str="Hello" 与 String str=new String(“Hello”)一样吗?
String str="Hello" 与 String str=new String(“Hello”)一样吗?
|
14天前
|
Java 索引
Java String应用与开发
Java String应用与开发
22 0
|
18天前
Swagger2异常:java.lang.NumberFormatException: For input string: ““
Swagger2异常:java.lang.NumberFormatException: For input string: ““
20 1
|
18天前
|
缓存 安全 Java
【Java基础】String、StringBuffer和StringBuilder三种字符串对比
【Java基础】String、StringBuffer和StringBuilder三种字符串对比
9 0
|
18天前
|
存储 缓存 Java
|
19天前
|
Java API 索引
Java基础&API(2) String、StringBuilder详解
Java基础&API(2) String、StringBuilder详解