What is Varargs in Java?

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

Varargs also known as variable arguments is a method in java that takes input as a variable number of arguments. It is used for simplifying a method that requires a variable number of arguments.

Importance of Java Varargs

  • In Java, we can’t declare a method with a variable number of arguments until JDK 4. New methods were created in case of changes. Due to this, the length of the code was increased making it difficult to read. By the introduction of varargs these issues are solved.
  • The variable-length arguments used to be handled in two ways before JDK 5, first is by using an overloaded method and the second is by putting the arguments in an array and then passing this array to a method.
  • To solve all these problems, in JDK vararags were introduced which allowed declaring of methods with a variable number of arguments. It is by far the simplest and better option.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Syntax of Varargs

The varags method is implemented using a single dimension array internally. Hence, arguments can be differentiated using an index. A variable-length argument can be specified by using three-dot (...) or periods.

In this syntax, it tells the compiler that the method funMethod() can be called with zero or more arguments due to which, ‘a’ is declared as type int[] implicitly.

Examples

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science

Working of Varargs

Implementing a method by using varargs in Java. In this, we will be creating a metod that takes multiple arguments in the method parameter, do the computation and returns the output.

Output:

Explanation:

  • This syntax (...) tells the compiler that varargs have been used inside a method.
  • The arguments are stored inside an array ‘a’ internally. On an array, variable ‘a’ is operated whose data type is int.
  • This means that only int data type is accepted. To check the number of arguments or to find the length of an array, the a.length method can be used.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

format() Method

Java format() method is widely used for formatting the string. There are many things that can be done using this method. For instance, concatenating the string and formatting the output of concatenated string.

Syntax:

This returns a formatted string of specified format and locale.

This returns a formatted string of specified locale and arguments.

Erroneous Varargs Example

1. Specifying two varargs in a single method

2. By specifying varargs as the first parameter instead of last

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Overloading Varargs Methods

It is similar to that of overloading methods. Except in this, the varargs method is overloaded.

Example:

Output:

Things to Remember while using Varargs

  • Always keep varargs at the end while defining the signature of the method else it will get executed every time before the default function.
  • There should only be one varargs parameter in the method.

OR

Conclusion

  • Varargs also known as variable arguments is a method in java that takes input as a variable number of arguments. It is used for simplifying a method that requires a variable number of arguments.
  • In java, we can’t declare a method with a variable number of arguments until JDK 4. New methods were created in case of changes. Due to this, the length of the code was increased making it difficult to read. By the introduction of varargs these issues are solved.
  • The variable-length arguments used to be handled in two ways before JDK 5, first is by using an overloaded method and the second is by putting the arguments in an array and then passing this array to a method.
  • To solve all these problems, in JDK vararags were introduced which allowed declaring of methods with a variable number of arguments. It is by far the simplest and better option.
  • The varags method is implemented using a single dimension array internally. Hence, arguments can be differentiated using an index. A variable-length argument can be specified by using three-dot (...) or periods.
  • Things to remember while using Varargs:
    • Always keep varargs at the end while defining the signature of the method.
    • There should only be one varargs parameter in a method.