Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Variable Arguments

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 292
    Comment on it

    Variable Arguments :

    We can pass variable number of arguments to a function in Java. Variable arguments means passing different number of arguments to same function on different call. Variable number of arguments are represented by elipsis(...) and known as 'variable arity method'. It eliminates the need of overloading methods to pass different number  of arguments.

    Syntax:

    1. returnType methodName(data_type... variableName){}

    Example:

    1. class Varargs1{
    2. static void show(String... values){
    3. System.out.println("show method invoked ");
    4. for(String s:values){
    5. System.out.println(s);
    6. }
    7. }
    8. public static void main(String args[]){
    9. show(); //zero argument
    10. show("hey"); //one argument
    11. show("my","name","is","varargs");//four arguments
    12. }
    13. }

    Output:

    1. show method invoked
    2. show method invoked
    3. hey
    4. show method invoked
    5. my
    6. name
    7. is
    8. varargs

    In the above example we are passing different number of arguments to show method.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Reset Password
Fill out the form below and reset your password: