I was trying to some examples and was not able to understand the reason for following code error in the following example
public class Test {
public static void print(Integer object){
System.out.println("object");
}
public static void print(String string){
System.out.println("String");
}
public static void main(String... args){
print(null);
}
}
I am getting error as: The method print(Integer) is ambiguous for the type Test
but when I am trying the same example by changing Integer to Object the code compiles fine and gives output as String
public class Test {
public static void print(Object object){
System.out.println("object");
}
public static void print(String string){
System.out.println("String");
}
public static void main(String... args){
print(null);
}
}
Can anyone please help me in understanding why the method with Object in its signature is required when the output is from the method which is having String in it. And what is the reason for the ambiguous type error.