Java Varargs: Use & Examples

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Sometimes we don't know how many arguments we'll be sending a given method. This lesson will cover the Java varargs concept and provide working code examples for passing varying arguments.

Java Varargs

In order to explain the concept of variable arguments, we don't have to look any further than the core method of our programs, the main method. The varargs concept means that a method can take any number of arguments, from zero to a dozen.

static void showMsg(String... vars) {
  System.out.println("Varargs Output");
}

Next, we'll call this method twice. Once with no arguments, and then with four.

public static void main(String[] args) {
  // no argument
  showMsg();
  // four arguments
  showMsg("Hi", "Let's", "Play", "Golf");
}

Loop Through the Arguments

The previous example showed the basics of the concept. However, we still didn't really do anything. Next, let's walk through each of the arguments and display the result. This functionality will come in quite handy very shortly. Once we pass the arguments to the showMsg method, we can cycle through them with a simple 'for' loop. Since the variables/arguments are Strings, we need to specify this in the loop, provide a counter (s) and then the variable we are looping over. In this case, it is vars, which is the variable that holds our argument.

public void showMsg(String... vars) {
  System.out.println("Varargs Output");
  // loop through arguments
  for(Strings s : vars) {
   System.out.println("Arg = " + s);
  }
}

When we compile and run this code, the following output is displayed. Recall that we called this method twice, once with no arguments, and once with four. The first message will display twice.


Java varargs loop args output


Let's look at one more example, using integers instead of strings. The following code combines both the method and the code that calls the method.

static void updateStuff(int... ints) {
  System.out.println("Ints!");
  for(int x: ints) {
   System.out.println("Ints = " + x);
  }
}
public static void main(String[] args) {
  updateStuff(13, -2333, 0);
}

When the program runs, the following output is displayed:


Java varargs int output


Note that the values passed must all be the same type! You can't mix doubles or a string or any other type. There is a way to mix the types in a method and still use the varargs functionality. We'll talk about that next.

Multiple Arguments

You can actually have your variable arguments along with other arguments. That is, you can pass your method a double, an int, and then a String using varargs. It might seem silly to have multiple arguments in a method that already takes multiple arguments. Consider this, the varargs piece can only accept one data type. If we want to send other types, we need more arguments. However, this also means that you will need to pass those arguments to the method each time.

Let's take a look at some code. The following adds some additional arguments to our showMsg method. Note that the String... vars command is the last command. When you have multiple arguments, the varargs functionality MUST be the last one.

static void showMsg(doubld d, int c, String... vars) {
  System.out.println('Varargs Output');
  // show the numbers
  System.out.println('Double = ' + d);
  System.out.println('Int = ' + c);
  // loop through the arguments
  for(String s: vars) {
   System.out.println('Arg = ' + s);
  }
}

Next, look at how we call this method. We will have to send it a double and an int, and then we can send whatever else we need/want to.

public static void main(String[] args) {
  showMsg(32.53, 15, "Hi", "Let's", "Play", "Golf", "Today");
}

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support