How does Java choose which constructor to use?
•
Java
I can't understand the output of the following program
public class Confusing { private Confusing(Object o) { System.out.println("Object"); } private Confusing(double[] dArray) { System.out.println("double array"); } public static void main(String[] args) { new Confusing(null); } }
The correct output is "double array" Why is this constructor chosen to be more specific than the other when both can accept null?
Solution
Even though both constructors can accept null, double [] inherits from Java Lang. object, so it is more specific
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码