I took the code from this link and modified some parts of it:
public static void main(String[] args) {
Byte i = 5;
byte k = 5;
aMethod(i, k);
}
//method 1
static void aMethod(byte i, Byte k) {
System.out.println("Inside 1");
}
//method 2
static void aMethod(byte i, int k) {
System.out.println("Inside 2");
}
//method 3
static void aMethod(Byte i, Byte k) {
System.out.println("Inside 3 ");
}
//method 4
static void aMethod(Byte i, Byte ... k) {
System.out.println("Inside 4 ");
}
The compiler gives error (The method is ambiguous for the type Overloading) for methods 1, 2 and 3 but not 4 (why?)
The answer lies in the mechanism which java uses to match method calls to method signatures. The mechanism is done in three phases, in each phase if it finds matching method it stops:
+phase one: use widening to find matching method (no matching methods found)
+phase two: (also) use boxing/unboxing to find matching method (method 1,2 and 3 match)
+phase three: (also) use var args (method 4 matches!)
(char)
method – TrimesterTestOverload()
isn't an actual problem. – Seepage