DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Formatting Strings in Java: String.format() Method
  • How to Fully Validate URLs in Java
  • The Pitfalls of Using Boolean Parameters in Methods: A Java Tutorial
  • DZone Community Awards 2022

Trending

  • AI's Dilemma: When to Retrain and When to Unlearn?
  • How To Replicate Oracle Data to BigQuery With Google Cloud Datastream
  • Why We Still Struggle With Manual Test Execution in 2025
  • Scalable System Design: Core Concepts for Building Reliable Software
  1. DZone
  2. Coding
  3. Java
  4. Uncommon Java Syntax: Ellipses…

Uncommon Java Syntax: Ellipses…

New programmers, and even some experienced ones, might not have experienced ellipses/varargs in Java yet. Here's a primer and some best practices.

By 
Niyi Oke user avatar
Niyi Oke
·
Dec. 07, 17 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
25.9K Views

Join the DZone community and get the full member experience.

Join For Free

Existing since Java SE 5.0 the ellipsis, also known as varargs, is one of those rarely underutilized features of Java. My guess is many novice programmers, and indeed even some experienced ones, have yet to meet Mr. Ellipsis — "…". I for one didn’t come across this elegant feature until after a year of full-time programming with Java. So what is an ellipsis?

Defining Varargs

I could not find any clear definition from the Javadocs but from what I could gather online, ellipses (also officially known as varargs (Variable Arguments)) are a Java syntax that describes an argument in a method that can take in zero or many arguments. Confusing? Let’s look at an example.

Ordinarily, if we wanted to define a method taking in two parameters of the same type, we would have something like this.

public int sum(int a, int b) {
    Return a + b;
}

//A method to sum 10 numbers will look like this
public int sum(int a, int b, int c, int d, int e, int f, int g, int h, int I, int j) {
    //return the sum of a to j
}

//Although a more experienced developer might decide to use an array like this
public int sum(int[] numbers) {}


Benefits

The downside to using an array here is that first, the numbers will first have to be stored in an array either through initialization or by iterating through a loop and assigning numbers to array elements.

Although it is still true that multiple arguments must be passed in an array, the varargs feature automates and hides the process for us, giving us a more elegant solution such as the one below:

public int sum(int… numbers) {
    int sum = 0;
    for (int number: numbers) {
        sum += number;
    }

    return sum;
}

public static void main(String[] args) {
    System.out.println(sum());
    System.out.println(sum(1, 2));
    System.out.println(int[] {
        3,
        4,
        5
    });
    System.out.println(sum(6, 7, 8, 9, 10));
}


The output would be:

0

3

12

40


Notice in our third call of the method sum that we passed an array as argument for the method. This further proves that varargs still operate as an array behind the scenes.

Rules for Use

Before we run off to embrace this cool feature, there are some rules to its use we need to understand.

Rule 1: The Last Argument

Always use varargs as the last argument in a method. Using it as the first or middle arguments will create confusion and cause logical errors. For example:

public int sum(int… numbers, int y, int z) {
    int sum = 0;
    for (int number: numbers) {
        sum += number;
    }
    return sum;
}

//Calling this method would look like this
int sum = sum(2, 3, 4, 5, 6);


From the above, which integers are part of the array and which two are for y and z arguments? It’s a contradiction that results in a compile-time error.

Rule 2: The One and Only Varargs

There must be not more than one varargs argument in a method. For example...

public int sum (int… numbers, String… names){}


...will lead to a compile-time error.

Rule 3: Do Not Overload

Varargs methods must not be overloaded. Otherwise, it creates confusion for the compiler, leading to a compile-time error. For example:

public int sum(int a, int… numbers){}
public int sum(int a, int b, int… numbers){}


In these examples above, rules 1 and 2 are not violated — but can you ascertain which method will be called?  

sum(2,3,4,5);
sum(1,2,3,4,5);


This dichotomy leads to a compile-time error.

Now you know the pros and cons and do’s and don’t’s of using varargs. Enjoy!

Java (programming language) Syntax (programming languages)

Opinions expressed by DZone contributors are their own.

Related

  • Formatting Strings in Java: String.format() Method
  • How to Fully Validate URLs in Java
  • The Pitfalls of Using Boolean Parameters in Methods: A Java Tutorial
  • DZone Community Awards 2022

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!