Normally, when you call a constructor or method for which multiple overridden versions might apply, Java will choose the most specific constructor or method. Section 15.12.2 of the Java Language Specification explains this in detail.
Suppose you have two overloaded methods, like this:
public void method(Object o) {
// ...
}
public void method(String s) {
// ...
}
When you call method(null)
, both these methods apply. Java chooses the most specific one, which is in this case the second method, that takes a String
- because String
is a more specific type than Object
.
However, sometimes the most specific constructor or method cannot be determined. If we look at the constructors of class String
that take one argument:
String(byte[] bytes)
String(char[] value)
String(String original)
String(StringBuffer buffer)
String(StringBuilder builder)
Note that there is no hierarchy between the types byte[]
, char[]
, String
, StringBuffer
and StringBuilder
, so it's not possible to say that one of these constructors is more specific than the others. So, the Java compiler doesn't know which constructor to choose and will give you an error.