Java中的float是什么? [英] What is float in Java?

查看:244
本文介绍了Java中的float是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这段代码:

  float b = 3.6; 

我得到这个:

<$ p $
错误:未解决的编译问题:
类型不匹配:无法从双精度转换为浮点数

为什么?什么是 float的定义

解决方案

一个十进制数为 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中的float是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆