简体   繁体   English

浮子怎么变成双子了?

[英]How did a float turn into a double here?

Edit: As always, great answer in under 5 minutes :) Turns out if I make a tiny change - make the F capital in "float", I'll get the output I expected. 编辑:与往常一样,在5分钟内给出了一个很好的答案:)事实证明,如果我做一个微小的更改-将F大写成“ float”,我将得到预期的输出。

class NumberMachine{
      public static void main(String [] args) {
        Integer wi1 = new Integer("420");
        int i = 101;
        Integer wi2 = i*420/101;
        if(wi1 == wi2) System.out.print(" =="); 
        if(wi1.equals(wi2)) System.out.print(" equal"); 
         float f = 1.23f; //if this were Float f..., it'd print Float, not double.
         new NumberMachine().printIt(f);
      }

      void printIt(Float f){
         System.out.println(" Float");
      }
      void printIt(double  d){
         System.out.println(" double");
      }
  }

The output is equal double , which makes no sense to me. 输出equal double ,这对我来说毫无意义。 I expected equal Float . 我期望equal Float If I comment out the 2nd printIt, then that's indeed the output. 如果我注释掉第二个printIt,那确实是输出。 I just don't know why, when faced with a choice between the two printIt, the compiler ignored the one whose parameter matched perfectly. 我只是不知道为什么,当面对两个printIt之间的选择时,编译器会忽略参数完全匹配的那个。

You get the result you do because boxing/unboxing was added late in Java's life and it was required that pre-existing code not be changed by the addition of the feature. 之所以可以得到结果,是因为装箱/拆箱是在Java的较晚时期添加的,并且要求不通过添加功能来更改预先存在的代码。 So when you pass in a primitive float to printIt, it gets coerced to a primitive double, because the alternative would mean old (pre-JDK1.4) code would act differently, which was an unacceptable possibility for Sun. 因此,当您将原始浮点数传递给printIt时,它将被强制转换为原始双精度点,因为替代方法将意味着旧的(JDK1.4之前的)代码将以不同的方式起作用,这对于Sun来说是不可接受的。

Basically, think if this was pre-JDK1.4 code where boxing was not an alternative, no way could the primitive double get coerced to a java.lang.Float. 基本上,请考虑一下,如果这是JDK1.4之前的代码,而装箱不是一种选择,那么根本不可能将原始的double强制转换为java.lang.Float。 Adding boxing can't be allowed to break that. 添加拳击不能打破它。

Reading the question it occurs to me from how you word it you may not see the difference between Float and float, because you refer to the change from capital F to lowercase F as a tiny change, when it really isn't so tiny. 从您的措辞上读到它对我的问题,您可能看不到Float和float的区别,因为您将从大写F到小写F的更改称为很小的更改,而实际上却不是那么小。 The lowercase version refers to a primitive numeric type, the uppercase version refers to an object that wraps the primitive numeric type, in order to allow numeric stuff to be used in general purpose things like collections. 小写版本是指原始数字类型,大写版本是指包装原始数字类型的对象,以便允许将数字内容用于诸如集合之类的通用用途。 Prior to JDK1.4 if you wanted to do something like this you had to manually write a line like 在JDK1.4之前,如果您想执行类似的操作,则必须手动编写如下行

myList.add(new Float(1.0F));

if you wanted to add a float value to a list. 如果要向列表添加浮点值。 The addition of boxing/unboxing in JDK1.4 tried to paper over this and have the compiler do this for us, but unfortunately you still have to understand the difference between the two to make sense of what's going on. JDK1.4中的装箱/拆箱功能试图解决这一问题,并让编译器为我们完成此操作,但是不幸的是,您仍然必须了解两者之间的区别,以了解发生的情况。

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

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