Why is this method of overloading a method with a variable number of parameters unacceptable?
void methodName(Integer ... args) { } void methodName(Integer[ ] args) { } Why is this method of overloading a method with a variable number of parameters unacceptable?
void methodName(Integer ... args) { } void methodName(Integer[ ] args) { } In fact, a variable number of arguments is nothing more than a decoration of the syntax - this is just an array of transmitted data, in this case integer numbers.
And then write
void methodName(Integer[ ] args) { } you are actually duplicating the signature one by one. One method is disclosed in the transfer of an array of integers and the other.
Two methods with the same signature cannot exist in the same class.
Inside Java, with its mechanisms, it twists this syntactic sugar called Integer... : it looks at the number of arguments, their type, and dynamically creates an array of the transmitted type, of size N T..e. as a result, the method will be passed to new Integer[] {num1, num2, num3, num3, и т.д.}; exactly the same as it will have to do in the second case
Integer... mechanisms Integer... at the beginning, it looks at the number of arguments, their type, and creates an array of the passed type, of size N T..e. as a result, the method will be passed to new Integer[] {num1, num2, num3, num3, и т.д.}; it is exactly the same as it will have to be done in the second case .. Only in the second you do it yourself, and in the first you will do everything for you .... syntactic sugar is ...... but the meaning does not change. The signature will remain the same - Alexey Shimanskybecause Integer... and Integer[] equivalent, and are arrays.
Source: https://ru.stackoverflow.com/questions/539055/
All Articles