What are variable length arguments?
What are variable length arguments?
Rating:
Variable length arguments are used to declare a method that takes a variable number of arguments. This is one of the features added in the J2SE 5 version. Varargs are declared using a data type followed by three periods (...). An example of variable length arguments is as follows:
Here, i represents an array of integers.
A method with a normal parameter can be written along with a variable length argument, the condition being that the varargs parameter should be the last parameter in the method. The following will throw a compile-time exception:
Instead, it should be written as follows:
Moreover, two variable length arguments cannot be written in one method. The example given below is incorrect:
Like any other method, a method with a varargs parameter can also be overloaded. However, it should be used with proper caution, as it may sometimes create ambiguity. For example, in the following syntaxes
Rating:
Other articles
- SCJP CX310-056 Short Notes: Exam passing tips
- What is the floor() method of the NavigableSet interface?
- Java Control Statements
- What is static import?
- What are the Closeable and Flushable interfaces?