- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Overloading Varargs Methods in Java
A method with variable length arguments(Varargs) can have zero or multiple arguments. Also, Varargs methods can be overloaded if required.
A program that demonstrates this is given as follows:
Example
public class Demo { public static void Varargs(int... args) { System.out.println("
Number of int arguments are: " + args.length); System.out.println("The int argument values are: "); for (int i : args) System.out.println(i); } public static void Varargs(char... args) { System.out.println("
Number of char arguments are: " + args.length); System.out.println("The char argument values are: "); for (char i : args) System.out.println(i); } public static void Varargs(double... args) { System.out.println("
Number of double arguments are: " + args.length); System.out.println("The double argument values are: "); for (double i : args) System.out.println(i); } public static void main(String args[]) { Varargs(4, 9, 1, 6, 3); Varargs('A', 'B', 'C'); Varargs(5.9, 2.5); } }
Output
Number of int arguments are: 5 The int argument values are: 4 9 1 6 3 Number of char arguments are: 3 The char argument values are: A B C Number of double arguments are: 2 The double argument values are: 5.9 2.5
Advertisements