简体   繁体   English

java中0和0.有什么区别?

[英]What is the difference between 0 and 0. in java?

I don't actually have the specific code, but me and a friend had exactly the same code except he used 0. and I used just 0 . 我实际上没有特定的代码,但是我和一个朋友使用的代码完全相同,只是他使用了0.而我只使用了0 Mine compiled, but his didn't until he removed the dot. 我的编译了,但是直到他删除了点之后他才开始编译。 But now, I've initialised a variable double x = 0. by accident, forgot to delete the dot, and it compiled! 但是现在,我已经初始化了一个变量double x = 0. 。偶然地,忘了删除点,然后编译了它! Why? 为什么?

Making a guess here, you/your friend probably had a datatype that doesn't allow floating points, like int and long 在这里猜测,您/您的朋友可能有一个不允许使用浮点数的数据类型,例如intlong

Datatypes like float and double will allow you to set a . floatdouble这样的数据类型将允许您设置一个.

Any number with a decimal point is considered a double literal (formally, a floating-point literal ). 任何带小数点的数字都被视为double文字(通常是浮点文字 )。 In that scenario, you would use it as you would a double. 在这种情况下,您将像使用两倍一样使用它。

The grammar that calls this out is thus, with optional parts demarcated in brackets: 因此,将其称为语法,并在方括号中标出了可选部分:

Digits . [Digits] [ExponentPart] [FloatTypeSuffix]

Just like you wouldn't be able to do this... 就像您将无法执行此操作...

int num = 10.6;

...you aren't able to do this... ...您无法执行此操作...

int num = 10.;

...because that's a double . ...因为这是double

Let's write an example program. 让我们编写一个示例程序。

class A {
    public static void main(String args[]) {
        int a = 1;
        int b = 2.;
        double c = 3;
        double d = 4.;
    }
}

Three of these assignments are fine, but one triggers a compilation error: 这些分配中的三个分配很好,但其中一个会触发编译错误:

A.java:4: error: possible loss of precision
        int b = 2.;
                ^
  required: int
  found:    double
1 error

So what's going on? 发生什么了? 1 and 3 are integer constants; 13是整数常数; 2. and 4. are floating point constants. 2.4.是浮点常量。 Integer values are automatically converted to floating point when you try to use an integer in a context that requires a floating point value. 当您在需要浮点值的上下文中尝试使用整数时,整数值会自动转换为浮点。 That's possible because all integer values can be represented in a floating point type¹. 这是可能的,因为所有整数值都可以用浮点类型¹表示。

On the other hand, int b = 2. is invalid, because in general floating point values cannot be represented in an integer type. 另一方面, int b = 2.是无效的,因为通常不能以整数类型表示浮点值。 For example the value 2.1 cannot be represented as an int (or indeed any other integer type). 例如,值2.1不能表示为int (或实际上任何其他整数类型)。 Java does not automatically convert values in a way that changes them, so it rejects any attempt to assign a floating point value to an integer variable. Java不会以改变值的方式自动转换值,因此它拒绝任何将浮点值分配给整数变量的尝试。 More generally, Java rejects any attempt to use a floating point value when an integer is expected. 更一般而言,当期望使用整数时,Java会拒绝使用浮点值的任何尝试。 Java's rules are based on types, not on specific values, so there is no exception even for the case of 2. where the value could be intepreted as fitting an integer type². Java的规则是基于类型的,而不是特定的值,所以没有即使的情况下例外2.当值可以作为intepreted装修整数type²。

You can force a conversion by using a cast. 您可以使用强制转换。 When a floating point value is cast to an integer type, it is rounded towards zero (ie positive values are rounded down, negative values are rounded up). 将浮点值转换为整数类型时,会将其四舍五入为零(即,将正值四舍五入,将负值四舍五入)。

int b = (int)2.;

¹ In the case of int and double , all int values can be represented exactly by a double . ¹ 对于intdouble ,所有int值都可以由double精确表示。 This does not apply to some other combinations of integer and floating point types: for example, some large values of type long cannot be represented exactly by a value of type double ; 这不适用于整数和浮点类型的某些其他组合:例如,一些long类型的大值不能用double类型的值精确表示; but they can be represented approximately, and that's good enough because floating point values are approximations in the first place. 但它们可以近似表示,这已经足够了,因为浮点值首先是近似值。
² But actually the value does not fit: 2.0 means “approximately 2” (because all floating point values are approximations), whereas 2 means “the integer 2 (exactly)”. ² 但是实际上该值不适合: 2.0表示“大约2”(因为所有浮点值都是近似值),而2表示“整数2(完全)”。

As @Turing85 commented, 0 is an integer and 0.(zero dot) is a double). 正如@ Turing85所评论的,0是整数,0。(零点)是双精度型。 Most likely your friend was using an integer variable to store double value. 您的朋友很可能使用整数变量来存储双精度值。 For example 例如

int zeroInteger = 0.;

Won't work, as (0.) is a double literal, it can not be assigned to an integer variable, at least not without explicit typecasting. 不会起作用,因为(0.)是双精度字面量,因此不能将其分配给整数变量,至少在没有显式类型转换的情况下至少不能如此。

double zeroDouble = 0.;

This should work as the variable is of double type. 这应该可以工作,因为变量是double类型。

Any character sequence that represents a value but not stored in a variable is called a literal. 表示值但未存储在变量中的任何字符序列称为文字。 Following are some valid Java literals: 以下是一些有效的Java文字:

  1. "hello" “你好”
  2. 1234, 0x12, 012, 1234L 1234、0x12、012、1234L
  3. 2.3, 1E11, 2.3D 2.3、1E11、2.3D

Since Java supports multiple data types for Integer (byte, short, int, long) and Floating point (float, double) numbers it has to decide up front which type to assign to a literal value. 由于Java支持整数(字节,短整数,整数,长整数)和浮点数(浮点,双精度)数字的多种数据类型,因此Java必须预先确定将哪种类型分配给文字值。

In case of integer literal default data type is integer and in case of floating point number default data type is double . 如果是整数,则默认数据类型为integer ;如果是浮点数,则默认数据类型为double

Since default data type of the floating point literal is double, Java would give an error if we try to assign it to a float variable. 由于浮点文字的默认数据类型为double,因此如果我们尝试将其分配给float变量,Java将给出错误消息。

float myFloat = 2.3;

This would give an error. 这会产生错误。 Correct way of doing it would be to typecast the literal to float or just append "f" or "F" to it. 正确的做法是键入文字以使其浮动或仅在其后附加“ f”或“ F”

float myFloat = 2.3f; // or 2.3F

Above is the correct way. 以上是正确的方法。

Same goes for the integer literal too. 整数文字也是如此。 If we try to assign an integer literal to a byte or short variable Java would produce an error. 如果我们尝试将整数文字分配给字节或短变量,Java将会产生错误。 However here it does something intelligent by actually checking if the assigned value is outside the range of the variable then only it produces error. 但是,在这里它通过实际检查分配的值是否在变量的范围之外来执行某些智能操作,仅会产生错误。 For example. 例如。

byte someByte = 127;// is ok
byte someByte = 128; // is not ok

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM