問(wèn)題描述
我寫(xiě)了這段代碼:
float b = 3.6;
我明白了:
<上一頁(yè)>錯(cuò)誤:未解決的編譯問(wèn)題:類型不匹配:無(wú)法從 double 轉(zhuǎn)換為 float為什么?float
的定義是什么?
在 Java 中,當(dāng)您鍵入一個(gè)十進(jìn)制數(shù)為 3.6
時(shí),它會(huì)被解釋為一個(gè) double
.double
是 64 位精度的 IEEE 754 浮點(diǎn),而 float
是 32 位精度的 IEEE 754 浮點(diǎn).由于 float
不如 double
精確,因此無(wú)法隱式執(zhí)行轉(zhuǎn)換.
如果你想創(chuàng)建一個(gè)浮點(diǎn)數(shù),你應(yīng)該以 f
結(jié)束你的數(shù)字(即:3.6f
).
更多解釋請(qǐng)參見(jiàn)Java教程的原始數(shù)據(jù)類型定義.
I wrote this code:
float b = 3.6;
and I get this:
Error:Unresolved compilation problem: Type mismatch: cannot convert from double to float
Why? Whats the definition of float
?
In Java, when you type a decimal number as 3.6
, its interpreted as a double
. double
is a 64-bit precision IEEE 754 floating point, while float
is a 32-bit precision IEEE 754 floating point. As a float
is less precise than a double
, the conversion cannot be performed implicitly.
If you want to create a float, you should end your number with f
(i.e.: 3.6f
).
For more explanation, see the primitive data types definition of the Java tutorial.
這篇關(guān)于Java中的浮點(diǎn)數(shù)是什么?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!