简体   繁体   English

当我们将int参数传递给重载方法时会发生什么,其中float作为一个方法的参数而另一个方法具有双重参数

[英]What happens when we pass int arguments to the overloading method having float as a parameter for one method and another having double param

In overloading concept, i am having one doubt, that is . 在重载概念中,我有一个疑问,就是这样。 when i comes to overload the method with int value the method call's the float parameter method rather the double parameter method. 当我使用int值重载方法时,方法调用的是float参数方法,而不是double参数方法。

void method1(float f){
System.out.println('float');
}

void method1(double f){
System.out.println('double');
}

method call: 方法调用:

method1(10);

output: float 输出:浮动

As stated in the java tutorials in this link A floating-point literal is of type float if it ends with the letter F or f; 如此链接中的java教程中所述,浮点字面值的类型为float,如果它以字母F或f结尾; otherwise its type is double and it can optionally end with the letter D or d. 否则它的类型是双倍的,它可以选择以字母D或d结尾。

For the above case the method call should call the double parameter method. 对于上面的情况,方法调用应该调用double参数方法。 But it call's float parameter method. 但它调用了float参数方法。

How the process of overloading taking place in this area?. 如何在这个区域发生超载过程?

Testing a variant of your code, except with a byte literal and overloaded methods with various combinations of short , int , and long appears to imply that the compiler chooses the "least widening" conversion if more than one is available. 测试代码的变体,除了byte文字和带有shortintlong各种组合的重载方法之外,似乎暗示如果有多个可用的转换器,编译器会选择“最小扩展”转换。

Thus: 从而:

  • Between a short and an int , if you call the overloaded method with a byte , the short variant will be chosen shortint ,如果使用byte调用重载方法,则将选择short变量
  • Between an int and a long , if you call the overloaded method with a byte or short , the int variant will be chosen intlong ,如果使用byteshort调用重载方法,则将选择int变量

And so forth. 等等。

Thus, because long can be widened to either float or double , and because the float conversion is the "least widening", the float overload is chosen. 因此,因为long可以加宽为floatdouble ,并且因为float转换是“最小加宽”,所以选择float过载。


I think this is because of the "choose the most specific overload" way that the compiler resolves multiple possible overloads. 认为这是因为“选择最具体的重载”方式,编译器解决了多个可能的重载。 From the JLS, section 15.12.2.5: 从JLS,第15.12.2.5节:

The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time error. 非正式的直觉是,如果第一个方法处理的任何调用都可以传递给另一个没有编译时错误的调用,那么一个方法比另一个方法更具体。

So by this, a method that takes a float is "more specific" than a method that takes a double because any invocation handled by a method that takes a float can always be handled by a method that takes a double , but not the other way around. 因此,采用float的方法比采用double的方法“更具体”,因为采用float的方法处理的任何调用总是可以通过采用double的方法来处理,但不是另一种方法周围。

as per http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html 根据http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html

5.1.2. 5.1.2。 Widening Primitive Conversion 扩大原始转换

19 specific conversions on primitive types are called the widening primitive conversions: 对原始类型的19个特定转换称为扩展原始转换:

byte to short, int, long, float, or double byte to short,int,long,float或double

short to int, long, float, or double 短,int,long,float或double

char to int, long, float, or double char到int,long,float或double

int to long, float, or double int到long,float或double

long to float or double 长期浮动或加倍

float to double 漂浮加倍

In Java,there is relation between sub class and super class and also ascending level for primitives from byte short.... to double. 在Java中,子类和超类之间存在关系,并且从字节short ...到double的基元也有升序。

The rule is, whenever there is ambiguity which overloaded method to choose, the most near one sub class overloaded method or nearest primitive in ascending order is chosen. 规则是,只要存在选择性重载方法的模糊性,就选择最接近的一个子类重载方法或最接近的基元。

暂无
暂无

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

相关问题 当以其他顺序使用参数时,它是否是方法重载? - Is it method overloading when having arguments in another order? 由于一种方法有参数,主函数不起作用? 方法 Height(int, int ) 不适用于参数 () - Main function not working due to one method having parameters ? The method Height(int, int ) is not applicable for arguments () Java方法重载-方法的输入参数是double和double,传递的参数是int和double - Java Method overloading- input parameters for method is double and double, Passed arguments are int and double 调用具有一个可选参数的方法 - Call a method having one optional parameter 如何将一类的arraylist对象传递给以对象为参数的通用方法 - how to pass a arraylist object of one class to generic method which is having object as its parameter in java IllegalStateException:在同一方法中有两次调用时缺少行为定义 - IllegalStateException: missing behavior definition when having a double call in same method 当我们明确调用Garbage collector方法时会发生什么? - What happens when we explicitly call Garbage collector method? 当我们调用ArrayList.add()方法时会发生什么? - What happens when we call ArrayList.add() method? 当我们在 onCreate() 方法中调用 finish() 时会发生什么? - What happens when we call finish() in onCreate() method? 当我们在同一方法上添加@GET和@Consumes时会发生什么 - What happens when we add @GET and @Consumes on the same method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM