[英]Why does java force me to add f when I use float?
I'm very new to programming. 我是编程新手。 I have the following code: 我有以下代码:
float f = 18.45f;
this works fine. 这很好。 If I change that to: 如果我将其更改为:
float f = 18.45;
java saying this as error: java说这是错误:
error: possible loss of precision
But its optional in terms of double
. 但是它的可选性是double
。 But in long
again I'm facing the same problem. 但是long
我都面临着同样的问题。
Why does java forces me to do so, but not in case with double
? 为什么Java会强制我这样做,但如果出现double
不能这样做?
In Java, 18.45
is a double
data type which holds 64-bit. 在Java中, 18.45
是保留64位的double
数据类型。 float
data type can hold up to 32-bit only. float
数据类型最多只能容纳32位。 Adding the extra f
makes it a float (float literal). 添加额外的f
使其成为浮点数(浮点文字)。
See Primitive Data Types for more details. 有关更多详细信息,请参见原始数据类型 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.