双精度 vs Eclipse 中的双精度浮点数(Double)

huangapple go评论69阅读模式
英文:

double vs Double as Floating Point Numbers in Eclipse

问题

我的问题有两个部分。

  1. 为什么在Eclipse中以下代码能正常工作?难道 "Double" 不是一个类吗?
    Double h = 2.5;
    double j = 2;
  1. 为什么上面的 "Double" 在我没有给它赋一个小数值时会报错,但是 "double" 不管我是否给它赋予小数值,都能正常工作?
英文:

My question is two-part.

  1. Why does the following work fine in Eclipse? Isn't "Double" a class?
    Double h = 2.5;
    double j = 2;
  1. Why does "Double" above give me an error when I don't assign a decimal value to it, but "double" is fine whether or not I assign a decimal value to it?

答案1

得分: 2

正如已经提到的,术语是自动装箱。原始类型的对象包装器将自动转换。

至于你的第二部分,

Double a = 2;

这是不起作用的,因为2不是double类型,自动装箱仅在相同类型之间起作用。在这种情况下,2 是一个int类型。

但如果你强制转换它,

Double a = (double)2;

就可以正常工作。

double a = 2;

可以工作,因为int可以自动转换为double。但反过来就不行。

int a = 2.2; // 不允许。

请查看转换部分。在Java语言规范中。警告有时阅读起来可能很困难。

修正后的答案。

在Java中,你可以进行向上或向下的强制转换,或者进行缩小或扩大的转换(从32位到16位)。我倾向于将其看作是丢失 vs 不丢失某些内容。在大多数情况下,如果在赋值过程中有可能丢失部分值,就需要进行强制转换,否则就不需要(见末尾的异常情况)。以下是一些示例。

long a = 2; // 2是一个整数,但转到long类型不会`丢失`精度。
int b = 2L; // 在这里,2是一个long,赋值是不允许的。尽管long类型的2将适合int类型,但仍然需要强制转换。
int b = (int)2L; // 可以,但显然是一个人为的情况。

浮点数同理。

float a = 2.2f; // 可以
double b = a; // 没问题,没有丢失精度
float c = b; // 不能这样做,因为需要进行强制转换。

double c = 2.2f; // 从float到double,同样没问题。
float d = 2.2; // 2.2默认为double类型,所以需要强制转换或者使用float标识。
float d = (float)2.2;

异常情况

int到float或从long到double的转换时不需要强制转换。然而,精度仍然可能丢失,因为浮点数只有24位精度,双精度数只有53位精度。

要查看ints的情况,你可以运行以下代码:

for (int i = Integer.MAX_VALUE; i > Integer.MAX_VALUE-100; i--) {
    float s = i;
    int t = (int)s; // 需要常规强制转换
    if (i != t) {
        System.out.println(i + " " + t);
    }
}
英文:

As was already mentioned, the term is autoboxing. The object wrappers for the primitive types will automatically convert.

As to your second part,

Double a = 2;

Doesn't work since 2 is not a double and the auto boxing only works between the same types. In this case 2 is an int.

But if you cast it.

Double a = (double)2;

works just fine.

double a = 2;

works because an int can be automatically converted to a double. But going the
other way doesn't work.

int a = 2.2; // not permitted.

Check out the Section on conversions. In the Java Language Specification. Warning that it can sometimes be difficult to read.

Amended Answer.

In java you can cast up or down or have narrowing or widening casts (going from a 32 bit to 16 bit) value is narrowing. But I tend to think about it is losing vs not losing something. In most cases if you have the potential to lose part of value in assignment, you need to cast, otherwise you don't (See exceptions at end). Here are some examples.

long a = 2; // 2 is an integer but going to a long doesn't `lose` precision.
int b = 2L; // here, 2 is a long and the assignment is not permitted.  Even 
            // though a long 2 will fit inside an int, the cast is still 
            // required.
int b = (int)2L;  // Fine, but clearly a contrived case

Same for floating point.

float a = 2.2f; // fine
double b = a;   // no problem, not precision lost
float c = b;    // can't do it, as it requires a cast.

double c = 2.2f; // a float to a double, again a not problem.
float d = 2.2;  // 2.2 is a double by default so requires a cast or the float designator.
float d = (float)2.2;

Exceptions

No cast is required when converting from int to float or long to double. However, precision can still be lost since the floats only have 24 bits of precision and doubles only have 53 bits of precision.

To see this for ints you can run the following:

		for (int i = Integer.MAX_VALUE; i > Integer.MAX_VALUE-100; i--) {
		    float s = i;
		    int t = (int)s; // normal cast required
		    if (i != t) {
		    	System.out.println (i + " " + t);
		    }
		}

答案2

得分: 0

Double是一个包装类,创建新的Double会将相同类型的原始变量转换为对象。对于Double h = 2,你正在将一个int包装成一个Double。由于包装只能在相同类型之间进行,如果你想让你的Double变量为2,则应该使用

Double h = 2.0;

英文:

Double is a wrapper class, creating a new Double casts a primitive variable of the SAME type into a Object. For Double h = 2, you are wrapping a int into a Double. Since wrapping only works between same types, if you want your Double variable be 2, then you should use

Double h = 2.0;

huangapple
  • 本文由 发表于 2020年4月11日 08:54:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/61150790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定