Posts

Java - Varargs

Welcome to Java and varargs (short for variable arguments). In a situation where it is needed to pass in an variable amount of arguments to a method, varargs is a good answer. Some portions of this article are the same as this one: C# params. This is because they are both written by me and cover very similair topics. The code in both examples has the same idea. This is done to create a consistency for both articles. With that in mind lets continue. Before we get to the code there are a couple things to keep in mind. The varargs parameter needs to be the last in line; Only one vararg parameter is allowed.  The basic syntax is as follows: ? 1 public void methodName(parameterType... parameterName){} Notice the three dots behind parameterType. This is to tell the compiler that you want to use variable arguments. So what does the compiler in the background? It gathers all of the values and puts them into an array. The type of params can be anything. You can
Recent posts