Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use variable Argument in java

    • 0
    • 2
    • 2
    • 2
    • 0
    • 0
    • 0
    • 0
    • 228
    Comment on it

    How to use variable argument

    Using variable argument in java method can be very important tool . As we can give multiple no of inputs to the methods using variable argument.

    The rules to define variable-length parameters in a method definition are as follows:

    1.There must be only one variable-length parameters list.

    2.If there are individual parameters in addition to the list, the variable-length parameters list must appear last inside the parentheses of the method.

    3.The variable-length parameters list consists of a type followed by three dots and the name.

    import java.io.*;
     class MyClass {
     public void printStuff( int... values) {
     for (int v : values ) {
     System.out.println(v);
     }
     }
     }
    

    In the above code method printStuff(int... values ) have a variable argument . therefore multiple no of integers can be passed to it. For example:

    class VarargTest {
     public static void main(String[] args) {
     MyClass mc = new MyClass();
     mc.printStuff(1);
     mc.printStuff(1,2);
     mc.printStuff(1,2,3);
     }
     }
    
    //output::1
    //1 2
    //1 2 3
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: