Showing posts with label methods with varargs. Show all posts
Showing posts with label methods with varargs. Show all posts

Jul 14, 2020

Methods with VARARGS in JAVA | Java Language | Coding Winds

METHODS WITH VARARGS


Varargs represents variable-length arguments and it was introduced by JDK -5 (J2SE 5.0), it makes the java code more flexible in terms of dealing with methods. So what are actually varargs? Varagrs are actually used to pass parameters without any limitations through a method, or we can say that they allow us to declare a method with unspecified number of parameters for a given type of argument. So let’s see the basic syntax of varargs in methods.

Syntax:

returnType   methodName(Type … arguments){

}

In the above syntax we can notice that after writing the type of the parameters (inside the parentheses) it is the part of the syntax to put three periods(.) followed by the arguments (or the name that we could use for all the parameters. Let’s see an example to understand the implementation.

 

Ex:

public class Coding_Winds {

 

    static void Students(String ...person){

   

        for(String name: person)

            System.out.println("Hello "+name);

           

        }

    public static void main(String[] args) {

       

         Students("John", "David", "Suhel");

 

        }

    }

 

Output:

 Hello John

 Hello David

 Hello Suhel

 

So as seen in the above example we never mentioned the number of parameters that we passed through the method, now let’s see another example which would be calling the methods with more or less parameters.

Ex:

public class Coding_Winds {

 

    static void Students(String ...person){

       

        for(String name: person)

            System.out.println("Hello "+name);

           

        }

    public static void main(String[] args) {

       

         Students("John", "David", "Suhel");

         Students("Ollie","Alex");

         Students("Shawn");

 

        }

    }

 

Output:

 

 Hello John

 Hello David

 Hello Suhel

 Hello Ollie

 Hello Alex

 Hello Shawn

 

 

Note that the arguments here can be considered as arrays and we can use the array methods with them.

Ex:

import java.util.*;

 

public class Coding_Winds {

 

    static void Students(String ...person){

        Arrays.sort(person);

        System.out.println("No. of students "+person.length);

       

        for(String name: person)

            System.out.println("Hello "+name);

           

        }

    public static void main(String[] args) {

       

         Students("Andrew", "Brice", "Camron", "David" , "Erik");

       

        }

    }

 

Output:

 

Hello Andrew

Hello Brice

Hello Camron

Hello David

Hello Erik

 

So as we can see that the arrays methods works fine with the argument in the above example.


Hope you are clear on this  topic do read our more articles on JAVA LANGUAGE.

If you still have any doubt on this topic then do come to us via email "sophomoretechs@gmail.com" or via Instagram "@coding.winds".


This article is SUBMITTED By : Pranjal Rai


Do subscribe to our daily blog update by clicking here.


Thank You!