有示例代码(如下)。jdk@11 和 jdk@17 的执行结果不同。将 BigDecimal(1000) 格式化为字符串后,白色字符看起来有所不同。最后的结果看起来是一样的——但是,它不是同一个字符串(比较字节和 Base64.encoded)。
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.Base64;
import java.util.Locale;
public class NumberFormatTest {
public static void main(String...args) {
Locale locale = new Locale("fr", "FR");
NumberFormat numberFormat = NumberFormat.getCurrencyInstance(locale);
BigDecimal value = BigDecimal.valueOf(1000);
String result = numberFormat.format(value); // <-- HERE!
System.out.println(result);
byte[] bytes = result.getBytes();
for( int i=0; i<bytes.length; i++) {
System.out.print(bytes[i]+", ");
}
System.out.println();
System.out.println(Base64.getEncoder().encodeToString(bytes));
}
}
jdk@11的结果与jdk@17的结果不同
jdk@11 的结果:
1 000,00 €
49, -62, -96, 48, 48, 48, 44, 48, 48, -62, -96, -30, -126, -84,
McKgMDAwLDAwwqDigqw=
jdk@17 的结果:
1 000,00 €
49, -30, -128, -81, 48, 48, 48, 44, 48, 48, -62, -96, -30, -126, -84,
MeKArzAwMCwwMMKg4oKs
jdk@11:
openjdk version "11.0.12" 2021-07-20
OpenJDK Runtime Environment Homebrew (build 11.0.12+0)
OpenJDK 64-Bit Server VM Homebrew (build 11.0.12+0, mixed mode)
jdk@17:
openjdk version "17.0.1" 2021-10-19
OpenJDK Runtime Environment Homebrew (build 17.0.1+1)
OpenJDK 64-Bit Server VM Homebrew (build 17.0.1+1, mixed mode, sharing)
系统:
MacOS
Catalina 10.15.7
这是预期的行为吗?