java method overload details

Distinguishing overload methods

If several methods have the same name, how can Java know which one you are referring to?The rule is simple: every overloaded method must have a unique list of parameter types, including the order of parameter types.Note: The return type of the method is not considered.

Example:

package s1;

public class O {
	public static void main(String[] args) {
		O1 o1 = new O1();
		o1.test(3);
		o1.test(1, "gui");
	}
}

class O1 {
	void test(int i) {
		System.out.println("test: int");
	}
	void test(long l) {
		System.out.println("test: long");
	}
	void test(int i, String s) {
		System.out.println("test: i, String");
	}
	void test(String s, int i) {
		System.out.println("test: String, i");
	}
}

Run result:

test: int
test: i, String

Base type overload

If not, the base type can be automatically promoted from a smaller base type to a larger base type. If there is no base type that can be converted to a larger base type, an attempt is made to convert to the object type corresponding to the base type (int attempts to convert to I).Nteger); attempts to convert to a Number object if the corresponding object type cannot be converted; attempts to convert to an Object object if the Number object cannot be converted.This process can cause some confusion once it involves overloading.

Here is an example of the basic type of conversion from a "smaller" type to a "larger":

package s1;

public class P {
	void f1(char x) { System.out.print("f1 char    ");}
	void f1(byte x) { System.out.print("f1 byte    ");}
	void f1(short x) { System.out.print("f1 short    ");}
	void f1(int x) { System.out.print("f1 int    ");}
	void f1(long x) { System.out.print("f1 long    ");}
	void f1(float x) { System.out.print("f1 float    ");}
	void f1(double x) { System.out.print("f1 double    ");}
	
	void f2(byte x) { System.out.print("f2 byte    ");}
	void f2(short x) { System.out.print("f2 short    ");}
	void f2(int x) { System.out.print("f2 int    ");}
	void f2(long x) { System.out.print("f2 long    ");}
	void f2(float x) { System.out.print("f2 float    ");}
	void f2(double x) { System.out.print("f2 double    ");}
	
	void f3(short x) { System.out.print("f3 short    ");}
	void f3(int x) { System.out.print("f3 int    ");}
	void f3(long x) { System.out.print("f3 long    ");}
	void f3(float x) { System.out.print("f3 float    ");}
	void f3(double x) { System.out.print("f3 double    ");}
	
	void f4(int x) { System.out.print("f4 int    ");}
	void f4(long x) { System.out.print("f4 long    ");}
	void f4(float x) { System.out.print("f4 float    ");}
	void f4(double x) { System.out.print("f4 double    ");}
	
	void f5(long x) { System.out.print("f5 long    ");}
	void f5(float x) { System.out.print("f5 float    ");}
	void f5(double x) { System.out.print("f5 double    ");}
	
	void f6(float x) { System.out.print("f6 float    ");}
	void f6(double x) { System.out.print("f6 double    ");}
	
	void f7(double x) { System.out.print("f7 double    ");}
	
	void testChar() {
		char x = 'x';
		f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);System.out.println();
	}
	void testByte() {
		byte x = 1;
		f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);System.out.println();
	}
	void testShort() {
		short x = 1;
		f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);System.out.println();
	}
	void testInt() {
		int x = 1;
		f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);System.out.println();
	}
	void testLong() {
		long x = 1;
		f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);System.out.println();
	}
	void testFloat() {
		float x = 1.0f;
		f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);System.out.println();
	}
	void testDouble() {
		double x = 1.0;
		f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);System.out.println();
	}
	
	public static void main(String[] args) {
		P p = new P();
		p.testChar();
		p.testByte();
		p.testShort();
		p.testInt();
		p.testLong();
		p.testFloat();
		p.testDouble();
	}
}

Run result:

f1 char    f2 int    f3 int    f4 int    f5 long    f6 float    f7 double    
f1 byte    f2 byte    f3 short    f4 int    f5 long    f6 float    f7 double    
f1 short    f2 short    f3 short    f4 int    f5 long    f6 float    f7 double    
f1 int    f2 int    f3 int    f4 int    f5 long    f6 float    f7 double    
f1 long    f2 long    f3 long    f4 long    f5 long    f6 float    f7 double    
f1 float    f2 float    f3 float    f4 float    f5 float    f6 float    f7 double    
f1 double    f2 double    f3 double    f4 double    f5 double    f6 double    f7 double    

The example above shows that a "smaller" base type automatically changes to a "larger" base type if no method is found that takes that base type as a parameter.Char types differ, and if no method is found that accepts exactly the char parameters, the char is promoted to int.The short type also differs, and if no method is found that accepts the short parameter exactly, it will be promoted to the int type.One thing to note here is that chars and shorts both account for 16 bits, but chars are unsigned and shorts are symbolic.So the data ranges they represent are different.

The following is an example of how a "smaller" base type can be converted not only to a "larger" base type, but also to an object type corresponding to the base type, or even to a Number or Object.Take the int type as an example, and the order of conversion is int->long->float->double->Integer->Number->Object (realize here that Integer inherits Number, Number inherits Object)

package s1;

public class Q {
	void f1(int x) { System.out.print("f1 int    ");}
	void f1(long x) { System.out.print("f1 long    ");}
	void f1(Integer x) { System.out.print("f1 Integer    ");}
	void f1(Number x) { System.out.print("f1 Number    ");}
	void f1(Object x) { System.out.print("f1 Object    ");}
	
	void f2(long x) { System.out.print("f2 long    ");}
	void f2(Integer x) { System.out.print("f2 Integer    ");}
	void f2(Number x) { System.out.print("f2 Number    ");}
	void f2(Object x) { System.out.print("f2 Object    ");}
	
	void f3(Integer x) { System.out.print("f3 Integer    ");}
	void f3(Number x) { System.out.print("f3 Number    ");}
	void f3(Object x) { System.out.print("f3 Object    ");}
	
	void f4(Number x) { System.out.print("f4 Number    ");}
	void f4(Object x) { System.out.print("f4 Object    ");}
	
	void f5(Object x) { System.out.print("f5 Object    ");}
	
	void test1() {
		int i = 1;
		f1(i);f2(i);f3(i);f4(i);f5(i);System.out.println();
	}
	
	public static void main(String[] args) {
		Q q = new Q();
		q.test1();
	}
}

Run result:

f1 int    f2 long    f3 Integer    f4 Number    f5 Object    

Basic Type Other

In conjunction with the code, explain what should be noted when using basic types (purely personal summary, you are welcome to add)

package s1;

public class R {
	void f1(char x) { System.out.print("f1 char    ");}
	void f1(byte x) { System.out.print("f1 byte    ");}
	void f1(short x) { System.out.print("f1 short    ");}
	void f1(int x) { System.out.print("f1 int    ");}
	void f1(long x) { System.out.print("f1 long    ");}
	void f1(float x) { System.out.print("f1 float    ");}
	void f1(double x) { System.out.print("f1 double    ");}
	
	public static void main(String[] args) {
		// Here the value 1 is converted to a char type
		char value1 = 1;
		// Compilation error: Here the value 22222222 has exceeded the maximum value of type char and is considered to be of type int
		char value2 = 22222222;
		// Compilation error: Floating point number 1.0 is not followed by type characters (f, d), defaulting to double(d)
		float value3 = 1.0;
		double value4 = 1.0;
		
		R r = new R();
		// There is no indication of what type the value 4 is, defaulting to the int type, so f1(int x) is called
		r.f1(4);
		// There is no indication of what type the value 1.0 is, and the default is the double type, so f1(double x) is called
		r.f1(1.0);
	}
}

 

70 original articles published. 4. 10,000 visits+
Private letter follow

Tags: Java

Posted on Sun, 12 Jan 2020 21:42:36 -0500 by xeidor