What is float in Java?(Java中的浮点数是什么?)
问题描述
我写了这段代码:
float b = 3.6;我明白了:
<上一页>错误:未解决的编译问题:类型不匹配:无法从 double 转换为 float
为什么?float的定义是什么?
在 Java 中,当您键入一个十进制数为 3.6 时,它会被解释为一个 double.double 是 64 位精度的 IEEE 754 浮点,而 float 是 32 位精度的 IEEE 754 浮点.由于 float 不如 double 精确,因此无法隐式执行转换.
如果你想创建一个浮点数,你应该以 f 结束你的数字(即:3.6f).
更多解释请参见Java教程的原始数据类型定义.
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 floatis 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.
这篇关于Java中的浮点数是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Java中的浮点数是什么?
基础教程推荐
- Java Swing计时器未清除 2022-01-01
- 多个组件的复杂布局 2022-01-01
- 大摇大摆的枚举 2022-01-01
- 验证是否调用了所有 getter 方法 2022-01-01
- 如何在 Spring @Value 注解中正确指定默认值? 2022-01-01
- 不推荐使用 Api 注释的描述 2022-01-01
- 如何在 JFrame 中覆盖 windowsClosing 事件 2022-01-01
- 从 python 访问 JVM 2022-01-01
- Java 实例变量在两个语句中声明和初始化 2022-01-01
- 在 Java 中创建日期的正确方法是什么? 2022-01-01
