主(数据)类型的重载

测试一:

       传递参数为int 型的常量;

public class Test {
	public static void main(String[] args) {
		testConstValue();
	}
	
	public static void testConstValue() {
		print("testing with 5");
		function1(5);
		function2(5);
		function3(5);
		function4(5);
		function5(5);
		function6(5);
		function7(5);
	}
	
	
	//
	public static void print(String s) {
		System.out.println(s);
	}
	
	
	//
	public static void function1(char x) {
		print("function1(char)");
	}
	
	public static void function1(byte x) {
		print("function1(byte)");
	}
	
	public static void function1(short x) {
		print("function1(short)");
	}
	
	public static void function1(int x) {
		print("function1(int)");
	}
	
	public static void function1(long x) {
		print("function1(long)");
	}
	
	public static void function1(float x) {
		print("function1(float)");
	}
	
	public static void function1(double x) {
		print("function1(double)");
	}
	
	
	//	
	public static void function2(byte x) {
		print("function2(byte)");
	}
	
	public static void function2(short x) {
		print("function2(short)");
	}
	
	public static void function2(int x) {
		print("function2(int)");
	}
	
	public static void function2(long x) {
		print("function2(long)");
	}
	
	public static void function2(float x) {
		print("function2(float)");
	}
	
	public static void function2(double x) {
		print("function2(double)");
	}
	
	
	//
	public static void function3(short x) {
		print("function3(short)");
	}
	
	public static void function3(int x) {
		print("function3(int)");
	}
	
	public static void function3(long x) {
		print("function3(long)");
	}
	
	public static void function3(float x) {
		print("function3(float)");
	}
	
	public static void function3(double x) {
		print("function3(double)");
	}
	
	
	//
	public static void function4(int x) {
		print("function4(int)");
	}
	
	public static void function4(long x) {
		print("function4(long)");
	}
	
	public static void function4(float x) {
		print("function4(float)");
	}
	
	public static void function4(double x) {
		print("function4(double)");
	}
	
	
	//
	public static void function5(long x) {
		print("function5(long)");
	}
	
	public static void function5(float x) {
		print("function5(float)");
	}
	
	public static void function5(double x) {
		print("function5(double)");
	}
	
	
	//
	public static void function6(float x) {
		print("function6(float)");
	}
	
	public static void function6(double x) {
		print("function6(double)");
	}
	
	//
	public static void function7(double x) {
		print("function7(double)");
	}
}

打印结果:

testing with t
function1(int)
function2(int)
function3(int)
function4(int)
function5(long)
function6(float)
function7(double)
根据打印结果分析: 因为 funtion1() 到 function4() 都能接收int型数据, 当传递int型数据时, 那么对应的参数列表为int型的方法被调用. 

分析function5(), 它只能接受long, float, double类型的数据. 也就是说传递给function5()方法的数据类型为int时, 实际上它是没有对应的方法去接受调用的, 这个时候编译器会对传递给function5()方法中的int型数据进行类型提升. 而到底提升为long? float? double??  根据结果可以看出, 如果有多种类型可以提升, 那么它会提升为最小的那个类型. 所以打印结果是 function5(long), 即function(long x) 这个方法被调用.. 其实分析function6()方法也可以看出来, function6()方法可以接受float , double类型数据, float比double小, 所以最终就把int型数据提升为float型. function6(float x)方法被调用..  function7()方法就一个可以接受double类型数据的方法. 所以int类型数据最后提升为double类型..  个人分析编译器这么做的目的是节省空间,提升效率, 因为当一个主(数据)类型做类型提升时, 虽然它的值的大小没有变话, 但是它所占用的内存空间其实变大了.

 

 

测试二:

       传递参数为char 类型;

public class Test {
	public static void main(String[] args) {
		testChar();
	}
	
	public static void testChar() {
		char x = 'X';
		print("testing with char");
		function1(x);
		function2(x);
		function3(x);
		function4(x);
		function5(x);
		function6(x);
		function7(x);
	}
	
	
	//
	public static void print(String s) {
		System.out.println(s);
	}
	
	
	//
	public static void function1(char x) {
		print("function1(char)");
	}
	
	public static void function1(byte x) {
		print("function1(byte)");
	}
	
	public static void function1(short x) {
		print("function1(short)");
	}
	
	public static void function1(int x) {
		print("function1(int)");
	}
	
	public static void function1(long x) {
		print("function1(long)");
	}
	
	public static void function1(float x) {
		print("function1(float)");
	}
	
	public static void function1(double x) {
		print("function1(double)");
	}
	
	
	//	
	public static void function2(byte x) {
		print("function2(byte)");
	}
	
	public static void function2(short x) {
		print("function2(short)");
	}
	
	public static void function2(int x) {
		print("function2(int)");
	}
	
	public static void function2(long x) {
		print("function2(long)");
	}
	
	public static void function2(float x) {
		print("function2(float)");
	}
	
	public static void function2(double x) {
		print("function2(double)");
	}
	
	
	//
	public static void function3(short x) {
		print("function3(short)");
	}
	
	public static void function3(int x) {
		print("function3(int)");
	}
	
	public static void function3(long x) {
		print("function3(long)");
	}
	
	public static void function3(float x) {
		print("function3(float)");
	}
	
	public static void function3(double x) {
		print("function3(double)");
	}
	
	
	//
	public static void function4(int x) {
		print("function4(int)");
	}
	
	public static void function4(long x) {
		print("function4(long)");
	}
	
	public static void function4(float x) {
		print("function4(float)");
	}
	
	public static void function4(double x) {
		print("function4(double)");
	}
	
	
	//
	public static void function5(long x) {
		print("function5(long)");
	}
	
	public static void function5(float x) {
		print("function5(float)");
	}
	
	public static void function5(double x) {
		print("function5(double)");
	}
	
	
	//
	public static void function6(float x) {
		print("function6(float)");
	}
	
	public static void function6(double x) {
		print("function6(double)");
	}
	
	//
	public static void function7(double x) {
		print("function7(double)");
	}
}
打印结果为:

testing with char
function1(char)
function2(int)
function3(int)
function4(int)
function5(long)
function6(float)
function7(double)

根据打印结果分析:   function1()有能接受char类型的方法, 所以就调用了function1(char x)方法;

                                    function2()方法接受byte, short , int, long, float, double类型的参数, 没有能接受char类型的方法. 这个时候会把char类型的数据转换为int型数据. 所以最终调用了function2(int x)这个方法, 打印结果是function2(int);

                                   function3(), function4()和function2()一样, 也是把char类型的数据转换为int 类型;

                                   function5() 有接受long, float, double 类型的方法, 所以 char类型最后转换成double类型了;

                                   function6() 只有接受float, double类型的方法, 所以char类型最后转换成float类型..

                                   function7() 只有double类型, 所以最后char类型转换成double类型..

综上可知: 当传递char类型的参数调用主(数据)类型重载的方法时, char类型会自动转换为 int , long, float, double, 具体转换为哪个, 优先顺序 int > long > float > double, 注意char类型不会转换为byte 和 short 类型..


测试三:

      传递参数为byte类型:

      

public class Test {
	public static void main(String[] args) {
		testByte();
	}
	
	public static void testByte() {
		byte x = 0;
		print("testing with byte");
		function1(x);
		function2(x);
		function3(x);
		function4(x);
		function5(x);
		function6(x);
		function7(x);
	}
	
	
	//
	public static void print(String s) {
		System.out.println(s);
	}
	
	
	//
	public static void function1(char x) {
		print("function1(char)");
	}
	
	public static void function1(byte x) {
		print("function1(byte)");
	}
	
	public static void function1(short x) {
		print("function1(short)");
	}
	
	public static void function1(int x) {
		print("function1(int)");
	}
	
	public static void function1(long x) {
		print("function1(long)");
	}
	
	public static void function1(float x) {
		print("function1(float)");
	}
	
	public static void function1(double x) {
		print("function1(double)");
	}
	
	
	//	
	public static void function2(byte x) {
		print("function2(byte)");
	}
	
	public static void function2(short x) {
		print("function2(short)");
	}
	
	public static void function2(int x) {
		print("function2(int)");
	}
	
	public static void function2(long x) {
		print("function2(long)");
	}
	
	public static void function2(float x) {
		print("function2(float)");
	}
	
	public static void function2(double x) {
		print("function2(double)");
	}
	
	
	//
	public static void function3(short x) {
		print("function3(short)");
	}
	
	public static void function3(int x) {
		print("function3(int)");
	}
	
	public static void function3(long x) {
		print("function3(long)");
	}
	
	public static void function3(float x) {
		print("function3(float)");
	}
	
	public static void function3(double x) {
		print("function3(double)");
	}
	
	
	//
	public static void function4(int x) {
		print("function4(int)");
	}
	
	public static void function4(long x) {
		print("function4(long)");
	}
	
	public static void function4(float x) {
		print("function4(float)");
	}
	
	public static void function4(double x) {
		print("function4(double)");
	}
	
	
	//
	public static void function5(long x) {
		print("function5(long)");
	}
	
	public static void function5(float x) {
		print("function5(float)");
	}
	
	public static void function5(double x) {
		print("function5(double)");
	}
	
	
	//
	public static void function6(float x) {
		print("function6(float)");
	}
	
	public static void function6(double x) {
		print("function6(double)");
	}
	
	//
	public static void function7(double x) {
		print("function7(double)");
	}
}
打印结果:

testing with byte
function1(byte)
function2(byte)
function3(short)
function4(int)
function5(long)
function6(float)
function7(double)

结果分析: 如果有接受byte类型数据的方法, 那么就会调用该方法. 如果没有的话就会把byte类型转换为 short, int, long, float, double,  优先级 short > int > long > float > double;


测试四:

      参数为short 类型:

public class Test {
	public static void main(String[] args) {
		testShort();
	}
	
	public static void testShort() {
		short x = 0;
		print("testing with byte");
		function1(x);
		function2(x);
		function3(x);
		function4(x);
		function5(x);
		function6(x);
		function7(x);
	}
	
	
	//
	public static void print(String s) {
		System.out.println(s);
	}
	
	
	//
	public static void function1(char x) {
		print("function1(char)");
	}
	
	public static void function1(byte x) {
		print("function1(byte)");
	}
	
	public static void function1(short x) {
		print("function1(short)");
	}
	
	public static void function1(int x) {
		print("function1(int)");
	}
	
	public static void function1(long x) {
		print("function1(long)");
	}
	
	public static void function1(float x) {
		print("function1(float)");
	}
	
	public static void function1(double x) {
		print("function1(double)");
	}
	
	
	//	
	public static void function2(byte x) {
		print("function2(byte)");
	}
	
	public static void function2(short x) {
		print("function2(short)");
	}
	
	public static void function2(int x) {
		print("function2(int)");
	}
	
	public static void function2(long x) {
		print("function2(long)");
	}
	
	public static void function2(float x) {
		print("function2(float)");
	}
	
	public static void function2(double x) {
		print("function2(double)");
	}
	
	
	//
	public static void function3(short x) {
		print("function3(short)");
	}
	
	public static void function3(int x) {
		print("function3(int)");
	}
	
	public static void function3(long x) {
		print("function3(long)");
	}
	
	public static void function3(float x) {
		print("function3(float)");
	}
	
	public static void function3(double x) {
		print("function3(double)");
	}
	
	
	//
	public static void function4(int x) {
		print("function4(int)");
	}
	
	public static void function4(long x) {
		print("function4(long)");
	}
	
	public static void function4(float x) {
		print("function4(float)");
	}
	
	public static void function4(double x) {
		print("function4(double)");
	}
	
	
	//
	public static void function5(long x) {
		print("function5(long)");
	}
	
	public static void function5(float x) {
		print("function5(float)");
	}
	
	public static void function5(double x) {
		print("function5(double)");
	}
	
	
	//
	public static void function6(float x) {
		print("function6(float)");
	}
	
	public static void function6(double x) {
		print("function6(double)");
	}
	
	//
	public static void function7(double x) {
		print("function7(double)");
	}
}
打印结果: 

testing with short

function1(short)

function2(short)
function3(short)
function4(int)
function5(long)
function6(float)
function7(double)


结果分析: 当传递参数为short时, 如果有short, 那么就调接受short参数类型的方法, 如果没有, short就会转换为 int, long, float, double类型. 优先级为 int > long > float > double


后续传递int, long, float, double 类型的数据都是一样的原理:

传递int型数据, 有接受int型数据的方法, 那么就优先调用这个方法, 如果没有, 就把int 转换为long, float, double, 优先级为 long > float > double;   

传递long型数据, 被调用方法的优先级   long > float > double;

传递float类型数据, float > double;

传递double类型数据, 只有接受double类型数据的方法才会被调用.


如果出现传递的参数类型比调用方法接受的参数取值范围大, 那么这个时候编译器会报错.. 比如传递一个double类型的参数, 调用func1(int x) 这个方法, 这个时候int类型数据不能接受double类型的数据, 那么就一定会报错. 如果要想不报错, 可以把double类型的参数强转成int 类型, 或者比int类型范围小的数据, 但是这可能会丢失精度.



     
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值