如果一个类有两个方法,分别带有不同参数,如下:
class A
{
long increment(long x) //long版本
{
return (x + 10);
} int increment(int x) //int版本
{
return x + 20;
} static public void main(String[] args)
{
A a = new A();
System.out.println( a.increment(10)); //如何判断?
}
}请问:如果参数类型非常接近,根据什么来判断调用的是哪个方法?
如果我将long和int换成float和double,调用a.increment(1.4)会得到社么结果呢?
有什么规律吗?
class A
{
long increment(long x) //long版本
{
return (x + 10);
} int increment(int x) //int版本
{
return x + 20;
} static public void main(String[] args)
{
A a = new A();
System.out.println( a.increment(10)); //如何判断?
}
}请问:如果参数类型非常接近,根据什么来判断调用的是哪个方法?
如果我将long和int换成float和double,调用a.increment(1.4)会得到社么结果呢?
有什么规律吗?
什么都不加。编译器默认为方法increment(double x)
1.4是double
12是int
2234567800也是int,不过它溢出了
数字10 是 int型
2.4 是double型
同样其它类型的值也类似,注意,这个是规定,没有理由可讲
如果你想用一个long型的10,必须这样用:10L
如果是float 数值后加f,如果是long数值后加l,
如果是个整数默认就是int,如果是个小数,默认就是double