简体   繁体   English

为什么我需要从double转换为int,反之亦然?

[英]Why do I need to cast from double to int, but not vice versa?

Let's say I have a double and an int. 假设我有一个double和一个int。

double x = 5;
int y = x;

When I do this, it throws an error because I need to explicitly cast from double to int like this, 当我这样做时,它会引发错误,因为我需要像这样将自己从double显式转换为int,

int y = (int) x;

for it to work. 为它工作。 But when I do 但是当我这样做

int x = 5;
double y = x;

No error is thrown, and it works perfectly fine. 没有引发任何错误,并且它工作得很好。 Is there any reasoning behind having to explicitly cast from double to int but not vice versa? 是否必须从double显式转换为int,反之亦然,是否有任何理由呢?

Double is more widened type than int. Double是比int更宽的类型。 According to specification you can assign the value in this order: 根据规范,您可以按以下顺序分配值:

byte -> short -> int -> long -> float -> double

The main idea: 主要思想:

A widening primitive conversion does not lose information about the overall magnitude of a numeric value. 不断扩大的原始转换不会丢失有关数值总体大小的信息。

But: 但:

A narrowing primitive conversion may lose information about the overall magnitude of a numeric value and may also lose precision and range. 变窄的原始转换可能会丢失有关数值总大小的信息,并且还会丢失精度和范围。

Because when you cast an integer to a double it's the same saying 1 or 1.0. 因为当您将整数转换为双精度数时,它表示的是1或1.0。 But when you cast from double to int it isn't the same saying 1.8 or 1 and then you have to explicitly cast it. 但是,当您从double转换为int时,说的是1.8或1是不一样的,那么您必须显式地对其进行转换。

Apart from losing accuracy, they have different sizes. 除了失去准确性外,它们还有不同的大小。

Think of trying to get a wardrobe out of a truck and into a car boot. 考虑尝试将衣柜从卡车中取出并放入汽车后备箱中。 That's not going to work, but taking a box from a car boot to a truck, will work. 那是行不通的,但是将箱子从汽车后备箱拿到卡车上将是可行的。

int is 2 to 4 bytes whereas a double is 8 bytes as explained here TutorialsPoint int是2到4个字节,而一个双为8个字节按此处的说明TutorialsPoint

According to Java Documentation : 根据Java文档

  • a int is stored using 32 bits of memory. 使用32位内存存储一个int。 It represents integers between -2^31 and a maximum value of 2^31-1 . 它表示-2 ^ 31和最大值2 ^ 31-1之间的整数
  • a double is stored using 64 bits of memory. 使用64位内存存储双精度数。 It represents floating numbers between 2^-1074 and (2-2^-52)·2^1023 . 它表示2 ^ -1074(2-2 ^ -52)·2 ^ 1023之间的浮点数

int to double 整数倍

Given the size of a double in memory, it is obvious that a double can contain an int (who can do more can do less). 给定内存中双精度型的大小,很明显双精度型可以包含一个int(可以执行更多操作的用户可以做更少的事情)。

double to int 双精度整数

This cast is not permitted since we lose information. 由于我们会丢失信息,因此不允许进行此演员表转换。 If we would like to convert for example a floating number like 1.1618, we lose information. 例如,如果我们要转换一个像1.1618这样的浮点数,则会丢失信息。 "Equivalents" of this double could be 1 or 2 but Java will never decide for you between the possibilities through the symbol '='. 这个双精度数的“等效项”可以是1或2,但是Java永远不会通过符号'='为您决定可能性。 You have to use a function like for example Math.round(1.1618); 您必须使用像Math.round(1.1618);这样的函数Math.round(1.1618); to choose which conversion you want to transform your double to an int. 选择您想将double转换为int的转换。

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

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