简体   繁体   English

在int中指定int到byte vs double浮点数

[英]assign int to byte vs double to float in java

1.when we assign double to float variable compiler gives us error 1.当我们为浮点变量编译器赋值时,编译器会给出错误

float f = 2753.2211;

possible loss of precision Required to cast. 可能丧失精度要求施放。

2.when we assign int to byte variable compiler don't gives us error 2.当我们将int分配给字节变量编译器时,不会给我们错误

byte b = 24;

OK

In second case data can also be lost.Then why casting explicitly is not necessary.? 在第二种情况下,数据也可能丢失。那么为什么没有必要明确地进行铸造。

The second case is explicitly allowed by the JLS as a special case. JLS明确允许第二种情况作为特例。 In JLS 5.2 , which deals with narrowing conversions, it says: 在处理缩小转换的JLS 5.2中 ,它说:

In addition, if the expression is a constant expression ( §15.28 ) of type byte, short, char, or int: 此外,如果表达式是byte,short,char或int类型的常量表达式(第15.28节 ):

  • A narrowing primitive conversion may be used if the type of the variable is byte, short, or char, and the value of the constant expression is representable in the type of the variable. 如果变量的类型是byte,short或char,则可以使用缩小的基元转换,并且常量表达式的值可以在变量的类型中表示。

... ...

In other words, for the non- long integer-like values, you can implicitly narrow them iff the value you're narrowing is a constant that fits within the type you're specifying. 换句话说,对于非long整数样值,你可以暗中缩小他们当且仅当你缩小值是你指定的类型中符合一个常数。

I'm guessing the same trick isn't applied to floats because floating point values are trickier than integer values. 我猜相同的技巧不适用于浮点数,因为浮点值比整数值更棘手。 For instance, 0.1 can't be exactly expressed in a floating point, while 1.0 can. 例如,0.1不能在浮点中精确表示,而1.0可以。 That means that double d = 0.1 isn't actually putting the value 0.1 into d -- just a value that's very close to 0.1. 这意味着double d = 0.1 实际上并没有将值0.1放入d - 只是一个非常接近0.1的值。 Given that, the "does it fit exactly" rule doesn't apply even to floating point literals, and the question of "does it fit exactly when we narrow" becomes trickier. 鉴于此,“它是否恰好适合”规则甚至不适用于浮点文字,并且“当我们缩小时它是否恰好适合”的问题变得更加棘手。 Your options would basically be: 您的选择基本上是:

  • always allow it (which could cause some surprising behavior if a value is significantly different than its literal representation) 总是允许它(如果一个值与其文字表示明显不同,这可能会导致一些令人惊讶的行为)
  • only allow it if the value can be exactly put in. This would look highly inconsistent: 只有在可以准确输入值时才允许它。这看起来非常不一致:
    • float f1 = 1.0 and double d1 = 1.0 both work float f1 = 1.0double d1 = 1.0都有效
    • double d2 = 0.1 works, but float f2 = 0.1 doesn't -- confusing! double d2 = 0.1有效,但float f2 = 0.1不会 - 令人困惑!
  • never allow it (slightly inconvenient, because you have to type the f char in the literal) 从不允许它(稍微不方便,因为你必须在文字中键入f char)

Given these options, it seems that the designers of the JLS chose the least of three not-great options. 鉴于这些选项,似乎JLS的设计者选择了三个不太好的选项中的至少一个。

Data isn't going to be lost in the second case. 在第二种情况下,数据不会丢失。

A byte comprises the values of -128 and 127 inclusive, so as long as your int fits within that range, no loss of precision can occur. 一个字节包含-128和127(包括-128和127)的值,因此只要您的int符合该范围,就不会发生精度损失。

Your second value is not a float -literal ; 你的第二个值不是float -literal ; by default, all floating point values in Java are double . 默认情况下,Java中的所有浮点值都是double You can explicitly make that a float by adding f to the end of it. 您可以通过在其末尾添加f来明确地使其成为float

float f = 2573.2211f;

int is a 32-bit signed integer, byte is an 8-bit signed integer. int是32位有符号整数,byte是8位有符号整数。 A byte runs from -128 to 127, while a int runs from -2147483648 to 2147483647 一个byte从-128到127运行,而int从-2147483648到2147483647

Precision isn't lost, but do not cast a big int into a small byte or you will lose data. 精度不会丢失,但不要将大型int转换为小byte否则您将丢失数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM