Varargs in Java: Variable argument method in Java 5

I think I am late for writing about varargs. The feature of variable argument has been added in Java 5 since its launch. Still I will write something about varargs. varargs has been implemented in many languages such as C, C++ etc. These functionality enables to write methods/functions which takes variable length of arguments. For example the popular printf() method in C. We can call printf() method with multiple arguments.
printf("%s", 50); printf("%d %s %s", 250, "Hello", "World");
Code language: Arduino (arduino)
Varargs was added in Java 5 and the syntax includes three dots (also called ellipses). Following is the syntax of vararg method.
public void testVar(int count, String... vargs) { }
Code language: Java (java)
Notice the dots … in above code. That mark the last argument of the method as variable argument. Also the vararg must be the last argument in the method. Now let us check simple hello world varargs code.
public class HelloWorldVarargs { public static void main(String args[]) { test(215, "India", "Delhi"); test(147, "United States", "New York", "California"); } public static void test(int some, String... args) { System.out.print("\n" + some); for(String arg: args) { System.out.print(", " + arg); } } }
Code language: Java (java)
In above code, the test() method is taking variable arguments and is being called from main method with number of arguments. So when should you use varargs? As a client, you should take advantage of them whenever the API offers them. Important uses in core APIs include reflection, message formatting, and the new printf facility. As an API designer, you should use them sparingly, only when the benefit is truly compelling. Generally speaking, you should not overload a varargs method, or it will be difficult for programmers to figure out which overloading gets called.

View Comments

  • Sir i dont knw why but its giving two errors. the same happens with any vargs program. i"m new to java programming so please help me out..
    the two errors are:
    [code language="java"]
    HelloWorldVarargs.java:8: (identifier expected)
    public static void test(int some, String... args)

    HelloWorldVarargs.java:14: (identifier expected)
    }

    2 errors.
    [/code]

    please help me out...

    • check the version of jdk if it is 1.6 or 7 you domt face problem since it( varags was introduced in 1.5 the implementation might be provided in 1.5 or 6 ,7

  • Is this explaneion enough? I am expecting more abt varargs....can u update?

    Thanks in advance

  • I have a question... the example you show looks like it boils down to an array of string....
    I know that variable arguments in c++ allow for mixed types:

    printf("%s %d %f", string1, int1, float1);

    I would assume that java also allows for this? and if so then how does one pull them out of the list?

  • The article was good and easy-to-understand... But it would be more helpful if you also gave output of the program...

Share
Published by
Viral Patel
Tags: Java java 5 java code varargs variable arguments

Recent Posts

  • Java

Java URL Encoder/Decoder Example

Java URL Encoder/Decoder Example - In this tutorial we will see how to URL encode/decode…

4 years ago
  • General

How to Show Multiple Examples in OpenAPI Spec

Show Multiple Examples in OpenAPI - OpenAPI (aka Swagger) Specifications has become a defecto standard…

4 years ago
  • General

How to Run Local WordPress using Docker

Local WordPress using Docker - Running a local WordPress development environment is crucial for testing…

4 years ago
  • Java

Create and Validate JWT Token in Java using JJWT

1. JWT Token Overview JSON Web Token (JWT) is an open standard defines a compact…

4 years ago
  • Spring Boot

Spring Boot GraphQL Subscription Realtime API

GraphQL Subscription provides a great way of building real-time API. In this tutorial we will…

5 years ago
  • Spring Boot

Spring Boot DynamoDB Integration Test using Testcontainers

1. Overview Spring Boot Webflux DynamoDB Integration tests - In this tutorial we will see…

5 years ago