Consider the following code in Java 5.0, why the function() method can not be overloaded?
At compile time a vararg is converted to an array (see USING THE VARARGS LANGUAGE FEATURE). The String... equates to a String array (String[]). Therefore, the compiler will treat the second function() method as the duplicate method in the class.
public class Program { void function(String... names){ System.out.println("in function(String... names)"); } void function(String[] names){ System.out.println("in function(String[] names)"); } }In the first function() method, the names argument is defined as type String.... This tells the compiler that calling code can pass a variable number of String parameters.
At compile time a vararg is converted to an array (see USING THE VARARGS LANGUAGE FEATURE). The String... equates to a String array (String[]). Therefore, the compiler will treat the second function() method as the duplicate method in the class.
Comments
Post a Comment