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
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
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

Migrate, Modernize and Build Java Web Apps on Azure: This live workshop will cover methods to enhance Java application development workflow.

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

Kubernetes in the Enterprise: The latest expert insights on scaling, serverless, Kubernetes-powered AI, cluster security, FinOps, and more.

A Guide to Continuous Integration and Deployment: Learn the fundamentals and understand the use of CI/CD in your apps.

Related

  • DZone Community Awards 2022
  • Reading an HTML File, Parsing It and Converting It to a PDF File With the Pdfbox Library
  • A Maven Story
  • JPA Criteria With Pagination

Trending

  • CI/CD Software Design Patterns and Anti-Patterns
  • Distributed Cloud Architecture for Resilient Systems
  • Modern Application Performance
  • How to Submit a Post to DZone
  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.

Niyi Oke user avatar by
Niyi Oke
·
Dec. 07, 17 · Tutorial
Like (4)
Save
Tweet
Share
22.3K 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

  • DZone Community Awards 2022
  • Reading an HTML File, Parsing It and Converting It to a PDF File With the Pdfbox Library
  • A Maven Story
  • JPA Criteria With Pagination

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • 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: