DEV Community

Cover image for USING “ELLIPSIS” IN YOUR CODE.
aidelojep
aidelojep

Posted on

USING “ELLIPSIS” IN YOUR CODE.

Hey! I'm Peter. I love to write tutorials and articles to help on some topics I find interesting in Java. If you have any questions about the article, leave a comment and I'll get back to you, or find me on twitter, LinkedIn or Github:
TWITTER LINKEDIN GITHUB

In this article, we are going to discuss a feature in Java or more generally programming, the term ellipsis is denoted as (. . .), it is originally a mathematics concept that is used to denote “....and so on.”
So basically, when applying this concept to programming, it is used to instruct a method to receive an unspecified number of arguments. In this article, our example focuses on arrays. Therefore, to use the concept of ellipsis(. . .) in array, all we need to do is:
specify the data type (int, double, string, boolean...),
then, we declare our ellipsis (. . .) next in the method’s parameter.
This indicates that the method receives an infinite number of variables with that particular data type.
This approach makes our program more concise and precise.

Ellipsis expression:

public class Varargs {

//calculating addition

   public  static void addition (int . . .**numbers**)
        {
 int **total** = 0;

    //calculating total using enhanced for loop 

   for (int p : numbers)
Enter fullscreen mode Exit fullscreen mode

total += p;
return total/numbers.length;
}
public static void main(String [] args) {
int p1 = 10;
int p2 = 20;
int p3 = 30;

//printing formatted data- ( initial values of p)

System.out.printf(“ p1 =%d%np2 =%d%np3 =%d%n);

//printing the sum p

System.out.printf(“The addition of these values(p1, p2 and p3) are:” addition(p1,p2,p3) );
}
}

image of varargs

NB:
// (double slash) indicates comments, comments in programming helps to explain the codes that we write so that they more readable.
For the purpose of this article, we would make use of commenting but most seasoned developers argue that a well written code does not require any commenting except it is not clean enough.
Commenting are not compiled with the source code, they only function to explain our code better.

CONCLUSION

This article helped to explain what an ellipsis does in a (Java) methods. Thanks for reading this article. I hope you like this article feel free to like, comment or share this article with your friends.

😄 Happy Coding…..Wireless API cares…

Top comments (0)