[英]Java - strange task for overloading methods
I have a task which should check if I understand overloading methods... 我有一个任务应该检查我是否了解重载方法...
but this task confused me much more 但是这个任务让我更加困惑
What is an explanation of this fact? 对这个事实有什么解释?
bar(i,c);
bar(l,c);
Output: 输出:
II
DL
Is there any scheme how to solve such tasks, any rule? 有没有计划如何解决这些任务,有没有规则?
public class Foo{
static void bar(int a, double b){
System.out.println("ID");
}
static void bar(int a, int b){
System.out.println("II");
}
static void bar(double a, long b){
System.out.println("DL");
}
static void bar(float... a){
System.out.println("FS");
}
static void bar(int a, byte b){
System.out.println("IB");
}
static void bar(double a, double b){
System.out.println("DD");
}
public static void main(String[] args) {
int i=0;
long l =0;
float f=0;
double d=0;
char c='0';
bar(i,f);
bar(d,i);
bar(i,c,i);
bar(i,c);
bar(l,c);
}
}
If there is a function that takes a long
as a parameter and there is not one taking a long
, and an int
is supplied, then that int
is promoted automatically to a long
type. 如果有需要的功能
long
作为参数,并没有一个采取long
,以及int
被提供,则该int
被自动提升为long
型。
If there is a function that takes a double
as a parameter and there is not one taking a double
, and a long
is supplied, then that long
is promoted automatically to a double
type. 如果有需要的功能
double
作为参数,并没有一个采取double
,和一个long
被提供,则该long
被自动提升为double
型。 Note that this conversion can cause you to lose precision: not all integers over the 53rd power of 2 can be represented exactly in a double
. 请注意,这种转换会导致您失去精度:并非53的2的幂的所有整数都可以用
double
精确表示。
These rules, of course, generalises to functions taking more than one parameter and explain the output your are observing. 这些规则当然可以推广到带有多个参数的函数,并解释您正在观察的输出。
From https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.2 来自https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.2
19 specific conversions on primitive types are called the widening primitive conversions:
关于原始类型的19种特定转换称为扩展原始转换:
byte to short, int, long, float, or double
字节到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
浮动到两倍
A widening primitive conversion does not lose information about the overall magnitude of a numeric value.
不断扩大的原始转换不会丢失有关数值总体大小的信息。
Basically since there's no long/char signature it will try to promote them to the above types and make decision based on that. 基本上,由于没有long / char签名,因此它将尝试将它们升级为上述类型,并据此做出决策。 There's a fairly elaborate algorithm here:
这里有一个相当复杂的算法:
http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12 http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12
And more specifically here: 更具体地说:
http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2 http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.