【问题标题】:primitive type float with unicode characters in javajava中带有unicode字符的原始类型浮点数
【发布时间】:2014-11-23 13:14:39
【问题描述】:
为什么下面的代码行不会给我任何编译错误
public class Test4
{
public static void main(String...a)
{
float f = \u0038;//Line 1
long L2 = 3L;
float fd = (float) 2.2;
char c = '\u005E';
byte e = 100;
}
}
第 1 行 float f = \u0038; 不会给我任何编译错误
如果是 unicode 字符,那么我们可以将它们与
float、double、int 和其他原始类型。
使用原始类型的 unicode 字符的标准是什么
如果是,那为什么我们可以使用它??
谢谢
【问题讨论】:
标签:
java
unicode
primitive
floating
【解决方案1】:
float 具有 32 位,范围在 ±1.4E-45 到 ±3.4028235E+38 之间
Unicode 有 16 位,范围介于 \u0000 到 \uFFFF 或 0 ... 65535
因此 float f = \u0038 有效,因为 float 格式大于 char 格式。所以什么都不会丢失。但是,如果您按照以下方式颠倒分配,您将看到强制转换的工作和格式的重要意义。
float f = \u0038; // OK because 16 bits fit a format of 32 bits
char x = (char) f; // no complaint because you "explicitly" cast f
// and take risk of losing precision
char y = f; // javac will complains "error: possible loss of precision"
// in order to warn you about the hidden "bugs" you might get...