Java - int/long, float/double

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1358864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:08:17  来源:igfitidea点击:

Java - int/long, float/double

javafloating-pointdoublelong-integerprimitive-types

提问by mk12

I understand that "2.5" is automatically a double, and to make it a float, I need to do "2.5F" (or should the F be lowercase?), and that I should use a float, say, if I had a constant that only ever needed 2 decimal spaces (like "0.08F" for Ontario PST tax), but I'm not sure whether "12" is an int or a long, but I know "12L" is a long, but "long l = 12" and "long l = 12L" seem to compile to the same thing, and I use long's if I want maximum non-floating point precision, and int if I know I won't need beyond int's limits.

我知道“2.5”自动是一个双精度数,要使它成为一个浮点数,我需要做“2.5F”(或者 F 应该是小写?),并且我应该使用浮点数,比如说,如果我有一个常数只需要 2 个小数位(例如安大略省 PST 税的“0.08F”),但我不确定“12”是整数还是长整数,但我知道“12L”是长整数,但“长整数” l = 12" 和 "long l = 12L" 似乎编译成同样的东西,如果我想要最大的非浮点精度,我使用 long ,如果我知道我不需要超出 int 的限制,我使用 int 。

Please correct me if there's something there that isn't right, and answer the quesions I have.

如果有什么不对的地方,请纠正我,并回答我的问题。

采纳答案by Yishai

12 as a constant in java is an int.

12作为java中的常量是一个int。

The reason long l = 12 compiles is that it is automatically widened to a long.

long l = 12 编译的原因是它会自动扩展为 long。

EDIT: Regarding your comment, there is nothing wrong with automatic widening, use whatever makes your code clearer, but just be aware of what is going on when you do math on primitives. For example:

编辑:关于您的评论,自动扩展没有任何问题,使用任何使您的代码更清晰的方法,但请注意在对基元进行数学运算时发生了什么。例如:

int i = 1213;
long l = 112321321L * i;

The long will have a very different value if you don't explicitly put that first number as a long because Java will treat it as integer math, and cause an overflow.

如果您没有明确地将第一个数字作为 long 放置,则 long 将具有非常不同的值,因为 Java 会将其视为整数数学,并导致溢出。

回答by Bill the Lizard

First, 12 is an int. Since you didn't specify a type, the compiler assumes int, just as it assumes doublewhen you don't specify a type for 2.5.

首先,12 是一个int. 由于您没有指定类型,编译器会假设int,就像double您没有为 2.5 指定类型时的假设一样。

The assignment

那作业

long x = 12;

compiles just fine because there is no loss of precision when the compiler implicitly upcasts the literal intvalue 12 to a long.

编译得很好,因为当编译器将文字int值 12隐式向上转换为 a时没有精度损失long

I always try to use uppercase type specifiers like 2.5F and 12L, simply because they're easier to read. 1 and l look much the same in the monospaced font that I prefer.

我总是尝试使用大写类型说明符,如 2.5F 和 12L,只是因为它们更易于阅读。1 和 l 在我喜欢的等宽字体中看起来非常相似。

回答by akf

It doesn't make a difference if you use lower or upper case when declaring your floats .

在声明floats时使用小写或大写没有区别。

...like "0.08F" for Ontario PST tax

...就像安大略省 PST 税的“0.08F”

If you are using these fields for currency, you should consider using BigDecimalinstead. See this explanationand its related links from Sun for more detail.

如果您将这些字段用于货币,则应考虑BigDecimal改用。 有关更多详细信息,请参阅Sun 的此说明及其相关链接。

回答by Joachim Sauer

Note that while intliterals will be auto-widened to longwhen assigning to a longvariable, you will need to use an explicit longliteral when expressing a value that is greater than Integer.MAX_VALUE(2147483647) or less than Integer.MIN_VALUE(-2147483648):

请注意,虽然在分配给变量时int文字将自动扩展为,但在表达大于(2147483647) 或小于(-2147483648)的值时,您将需要使用显式文字:longlonglongInteger.MAX_VALUEInteger.MIN_VALUE

long x1 = 12; //OK
long x2 = 2147483648; // not OK! That's not a valid int literal
long x3 = 2147483648L; // OK