[英]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
文字和带有short
, int
和long
各种组合的重载方法之外,似乎暗示如果有多个可用的转换器,编译器会选择“最小扩展”转换。
Thus: 从而:
short
and an int
, if you call the overloaded method with a byte
, the short
variant will be chosen 在short
和int
,如果使用byte
调用重载方法,则将选择short
变量 int
and a long
, if you call the overloaded method with a byte
or short
, the int
variant will be chosen 在int
和long
,如果使用byte
或short
调用重载方法,则将选择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
可以加宽为float
或double
,并且因为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.