这里的 float 是如何变成 double 的? [英] How did a float turn into a double here?

查看:39
本文介绍了这里的 float 是如何变成 double 的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一如既往,在 5 分钟内得到很好的答案:) 结果是,如果我做一个微小的改变 - 将 F 大写为浮动",我会得到我预期的输出.

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.

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");
      }
  }

输出是equal double,这对我来说没有意义.我期望 equal Float.如果我注释掉第二个 printIt,那么这确实是输出.只是不知道为什么,当面临两个printIt之间的选择时,编译器忽略了参数完美匹配的那个.

The output is equal double, which makes no sense to me. I expected equal Float. If I comment out the 2nd printIt, then that's indeed the output. I just don't know why, when faced with a choice between the two printIt, the compiler ignored the one whose parameter matched perfectly.

推荐答案

你会得到你所做的结果,因为装箱/拆箱是在 Java 生命周期的后期添加的,并且要求预先存在的代码不会因添加特征.因此,当您将原始浮点数传递给 printIt 时,它会被强制转换为原始双精度值,因为替代方法意味着旧的(JDK1.4 之前的)代码的行为会有所不同,这对于 Sun 来说是不可接受的.

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. 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.

基本上,如果这是 JDK1.4 之前的代码,其中拳击不是替代方案,那么原始 double 不可能被强制为 java.lang.Float.不允许添加拳击来打破这一点.

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. Adding boxing can't be allowed to break that.

阅读我从你的措辞中想到的问题,你可能看不到 Float 和 float 之间的区别,因为你将大写 F 到小写 F 的变化称为微小变化,而实际上并非如此微小的.小写版本是指原始数字类型,大写版本是指包装原始数字类型的对象,以便允许在集合等通用对象中使用数字内容.在JDK1.4之前,如果你想做这样的事情,你必须手动写一行

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. 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

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

如果您想将浮点值添加到列表中.在 JDK1.4 中添加装箱/拆箱试图掩盖这一点,让编译器为我们做这件事,但不幸的是,您仍然必须了解两者之间的区别才能理解发生了什么.

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.

这篇关于这里的 float 是如何变成 double 的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆