Float a = 1.2;
有一个错误,因为它将十进制作为double值,double是一个比float更大的数据类型.
现在,它将整数作为默认的int类型.那么,为什么以下代码没有给出任何错误?
Byte b = 20;
解决方法
A narrowing primitive conversion from
double
tofloat
is governed by the IEEE 754 rounding rules (07001). This conversion can lose precision,but also lose range,resulting in afloat
zero from a nonzerodouble
and afloat
infinity from a finite double. Adouble
NaN is converted to afloat
NaN and adouble
infinity is converted to the same-signedfloat
infinity.A narrowing conversion of a signed integer to an integral type T simply discards all but the n lowest order bits,where n is the number of bits used to represent type T. In addition to a possible loss of information about the magnitude of the numeric value,this may cause the sign of the resulting value to differ from the sign of the input value.
另见this thread.
原文地址:https://www.jb51.cc/java/120313.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。