简体   繁体   English

为什么铸造方向在原始类型中从大到小而在对象中从小到大?

[英]Why casting direction is big to small in primitive types and small to big with objects?

In Java we need casting when converting double(big in memory size) to Integer (smaller in memory size)在 Java 中,我们需要在将 double(memory 尺寸大)转换为 Integer(ZCD691B4957F098CD281Z 尺寸较小)时进行转换

int x = (int) 4.3;

But in case of objects if parent class is "Mammal"(small in memory size) and its subclass is "Human" (big in memory size since got more properties then mammal)但是在对象的情况下,如果父 class 是“哺乳动物”(在 memory 大小中小),其子类是“人类”(在 memory 大小中大),因为获得了更多属性然后哺乳动物

then然后

Mammal m = new Human(); //works without casting

but small to big conversion但从小到大的转换

Human h = (Human) m ; // needs casting 

Thanks in Advance.提前致谢。

Casting is not about the size of the object: it's about the range of the variable .铸造与 object 的大小无关:它与变量的范围有关

By 'range', i mean the variety of different values that the variable can contain. “范围”是指变量可以包含的各种不同值。 If you assign from one variable to another whose range is a superset of the first, you don't need to cast, because you know that the assignment will be okay.如果您从一个变量分配给另一个变量,其范围是第一个变量的超集,则不需要强制转换,因为您知道分配是可以的。 But when you assign from one variable to another whose range is a subset, you do need to cast, because the assignment might not be possible.但是,当您将一个变量分配给另一个其范围是子集的变量时,您确实需要进行强制转换,因为分配可能是不可能的。

Imagine you have two containers: a plastic tub and a wire shopping basket, of the same size.想象一下,您有两个容器:一个塑料桶和一个金属丝购物篮,大小相同。 Clearly, anything you can keep in the wire basket, you can keep in the tub.显然,任何你可以放在铁丝篮里的东西,你都可以放在浴缸里。 But not everything you can keep in the tub can be kept in the basket.但并不是所有可以放在浴缸里的东西都可以放在篮子里。 A pile of apples, you can.一堆苹果,你可以。 But a pile of raisins, you can't - they would fall through the holes in the basket.但是一堆葡萄干,你不能——它们会从篮子的洞里掉下来。 So, the range of things that the tub can hold is greater than the range of things the basket can hold, even though both are the same size.因此,浴缸可以容纳的物品范围大于篮子可以容纳的物品范围,即使两者的尺寸相同。

In that analogy, casting is like checking whether the thing you're moving will fit in the new container.在这个类比中,铸造就像检查你移动的东西是否适合新容器。 You don't need to check when moving things from the basket to the tub, but you do need to check when moving from the tub to the basket, otherwise you will end up with fruit all over the floor.将东西从篮子移到浴缸时不需要检查,但从浴缸移到篮子时需要检查,否则水果会弄得满地都是。

In your specific cases, we know that every human is a mammal, but that not every mammal is a human, so the range of a variable of type Mammal is greater than that of a variable of type Human.在您的具体情况下,我们知道每个人都是哺乳动物,但并非每个哺乳动物都是人,因此哺乳动物类型变量的范围大于人类类型变量的范围。 We also know that the range of a double (approximately 2^1024 - -(2^1024)) is greater than that of an int (2^31-1 - -2^31).我们也知道 double 的范围(大约 2^1024 - -(2^1024))大于 int 的范围(2^31-1 - -2^31)。 So, assigning from the former to the latter in either case requires a cast, but from the latter to the former does not.因此,在任何一种情况下从前者分配给后者都需要强制转换,但从后者分配给前者则不需要。

When you use primitive types, you have to explicitely cast when there is a chance you might loose information.当您使用原始类型时,您必须在有可能丢失信息时显式地强制转换。 For example, long is 64 bits and int is 32. Converting a long in an int can result in data loss (32 bits in this case).例如, long是 64 位, int是 32。在int中转换long会导致数据丢失(在这种情况下是 32 位)。

When dealing with objects, this is relative to polymorphism.在处理对象时,这与多态性有关。 The compiler is able to ensure that every Human is a Mammal .编译器能够确保每个Human都是Mammal No problem here.这里没问题。 But it is unable to ensure that every Mammal is a Human .但无法保证每一个Mammal都是Human You have to cast explicitely to convert the reference type.您必须显式转换才能转换引用类型。

You can see explicit casts as a way of saying to the compiler "I know you can't ensure this data conversion is safe, but I know what I am doing".您可以将显式转换视为对编译器说“我知道您无法确保此数据转换是安全的,但我知道我在做什么”的一种方式。

  1. In case of primitives memory comes into play.If you are going to store double into int as in your example, double takes more memory as compare to integer.So there is chance of data lose.So compiler throw error.If you cast yourself then you know what you are doing so compiler let that do.如果是原语 memory 起作用。如果您要像示例中那样将 double 存储到 int 中,则与 integer 相比,double 需要更多的 memory。你知道你在做什么,所以编译器让它去做。
  2. In case of object, No case of memory there.Just polymorphysm comes into play.So you can take a subclass object into supertype.在 object 的情况下,没有 memory 的情况。只是多态性起作用。所以你可以将子类 object 变成超类型。

    Hope you will get it.希望你能得到它。

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

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