java中默认整数为int型,小数为double型
自动类型转换(隐式类型转换)的次序为:
byte–>short–>char–>int–>long–>float–>double
1B 2B 1-2B 4B 8B 4B 8B
举个栗子:
public class MyTest {
public static void main(String[] args) {
Test test1 = new Test();
test1.test(5);
test1.test(5l);
test1.test(5.0);
test1.test(5.0, 6.0);
}
}
class Test {
void test(int x) //注释掉本方法,查看自动转换匹配
{
System.out.println("test(int):" + x);
}
void test(long x) {
System.out.println("test(long):" + x);
}
void test(double x) {
System.out.println("test(double):" + x);
}
String test(double x, double y) //方法重载与返回类型无关
{
System.out.println("test(double):x=" + x + ", y=" + y);
return "test(double):x=" + x + ", y=" + y;
}
}
运行结果如下:
如果注释掉 void test(int x),发生自动类型转换结果会变成这样: