简体   繁体   English

为什么在使用float时Java强制我添加f?

[英]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.

相关问题 为什么 java 在创建方法时不允许我使用方法 add()? - Why will java not let me use the method add() when creating a method? 当我不能使用Integer时,为什么Java没有告诉我? - Why is Java not telling me when I can't use Integer? 为什么在使用 Java DSL 时,我必须在入站 webflux 网关上使用.fluxTransform(f -> f)? - Why do I have to use .fluxTransform(f -> f) on an inbound webflux gateways when using Java DSL? 为什么Dagger 2强迫我使用@Provides而不是@Binds - Why does Dagger 2 force me to use @Provides instead of @Binds 当我使用编辑器代码生成器时,为什么eclipse会在构造函数中自动添加java super()方法? - Why does eclipse automatically add a java super() method in a constructor when I use the editors code generator? 为什么Spring强迫我使用静态变量? - Why Spring force me to use static variable? 当一个数字在 Java 中被认为是双精度时,为什么我们使用浮点数? - Why we use float when a number is considered as double in Java? 为什么 SongName[i]!= null 在它应该是 F 时评估为 T - Why does songName[i]!= null evaluates to T when it should be F 为什么 StreamEx 在收集到列表时强制我将“?扩展”添加到变量类型? - Why StreamEx force me to add "? extends" to variable type when collecting to list? 为什么当我使用带有文本的文本文件(如下所示)运行此代码时,它会给我一个 java.util.InputMismatchException - why is it giving me an java.util.InputMismatchException when i run this code when I use a text file with the text (shown underneath)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM