Skip to main content

Var arg method


UN initialized arrays
Example:
            class Test
                        {
                              int[] a;
                              public static void main(String[] args)
                                {
                                    Test t1=new Test();
                                    System.out.println(t1.a);//null
                                    System.out.println(t1.a[0]);//R.E:NullPointerException
                                }
                        }
Instance level:
Example 1:
int[] a;
System.out.println(obj.a);//null
System.out.println(obj.a[0]);//R.E:NullPointerException
Example 2:
int[] a=new int[3];
System.out.println(obj.a);//[I@3e25a5
System.out.println(obj.a[0]);//0
Static level:
Example 1:
static int[] a;
System.out.println(a);//null
System.out.println(a[0]);//R.E:NullPointerException
Example 2:
static int[] a=new int[3];
System.out.println(a);//[I@3e25a5
System.out.println(a[0]);//0
Local level:
Example 1:
int[] a;
System.out.println(a); C.E: variable a might not have been initialized
System.out.println(a[0]);
Example 2:
int[] a=new int[3];
System.out.println(a);//[I@3e25a5
System.out.println(a[0]);//0
· Once we created an array every element is always initialized with default values
irrespective of whether it is static or instance or local array.
Var- arg methods (variable no of argument methods) (1.5)
· Until 1.4v we can’t declare a method with variable no. Of arguments. If there is a
change in no of arguments compulsory we have to define a new method. This approach
increases the length of the code and reduces readability. But from 1.5 version onwards we
can declare a method with variable no. Of the arguments, such types of methods are called
var-arg methods.
· We can declare a var-arg method as follows.
· We can call or invoke this method by passing any no. Of int values including zero
number.
Example:
            class Test
                {
                    public static void methadone(int... x)
                      {
                         System.out.println("var-arg method");
                      }
                  public static void main(String[] args)
                     {
                          methodOne();
                          methodOne(10);
                          methodOne(10,20,30);
                     }
               }
Output:
var-arg method
var-arg method
var-arg method
· Internally var-arg parameter implemented by using single dimensional array hence
within the var-arg method we can different arguments by using index.
Example:
class Test
  {
           public static void sum(int... x)
               {
                    int total=0;
                    for(int i=0;i<x.length;i++)
                      {
                           total=total+x[i];
                      }
                         System.out.println("The sum :"+total);
                }
          public static void main(String[] args)
                  {
                       sum();
                       sum(10);
                       sum(10,20);
                       sum(10,20,30,40);
                  }
        }
Output:
The sum: 0
The sum: 10
The sum: 30
The sum: 100
Case 1:
Which of the following var-arg method declarations are valid?
1) methodOne(int... x)(valid)
2) methodOne(int ...x)(valid)
3) methodOne(int x...)(invalid)
4) methodOne(int. ..x)(invalid)
5) methodOne(int .x..)(invalid)
Case 2: We can mix var-arg parameter with general parameters also.
Example:
methadone(int a,int... b)
methadone(String s,int... x) valid
Case 3: if we mix the var-arg parameter with a general parameter then the var-arg parameter should be the last parameter.
Example:
methadOne(int... a,int b)(invalid)
Case 4: We can take only one var-arg parameter inside the var-arg method
Example:
methadone(int... a,int... b)(invalid)
Case 5:
    class Test
        {
              public static void methadone(int i)
                {
                    System.out.println("general method");
                }
              public static void methodOne(int... i)
               {
                    System.out.println("var-arg method");
               }
              public static void main(String[] args)
               {
                   methodOne();//var-arg method
                   methodOne(10,20);//var-arg method
                  methodOne(10);//general method
               }
       }
· In general var-arg method will get the least priority that is if no other method matched then
the only var-arg method will get the chance this is exactly same as default case inside a
switch.
Case 6: For the var-arg methods we can provide the corresponding type array as an argument.
Example:
                    class Test
                         {
                                     {
                                          System.out.println("var-arg method");
                                      }
                                  public static void main(String[] args)
                                     {
                                         methodOne(new int[]{10,20,30});//var-arg method
                                     }
                          }
Case 7:
    class Test
        {
             public void methodOne(int[] i){}
             public void methodOne(int... i){}
        }
Output:
Compile time error.
Cannot declare both methadone(int...) and methadone(int[]) in Test
Single Dimensional Array Vs Var-Arg Method:
Case 1: Wherever single dimensional array present we can replace with var-arg parameter.
Example:
   class Test
        {
              public static void main(String... args)
                  {
                       System.out.println("var-arg main method");//var-arg main method
                  }
         }
Case 2: Wherever var-arg parameter present we can’t replace with single dimensional array.
Example:
class Test
       {
            public static void methodOne(int[]... x)
               {
                    for(int[] a:x)
                      {
                           System.out.println(a[0]);
                      }
                }
             public static void main(String[] args)
                {
                       int[] l={10,20,30};
                       int[] m={40,50};
                       methodOne(l,m);
                }
        }
Output:
10
40
Analysis:

Comments

  1. Can you give me best example of var arg method

    ReplyDelete
    Replies
    1. class Test
      {
      public static void methodOne(int[]... x)
      {
      for(int[] a:x)
      {
      System.out.println(a[0]);
      }
      }
      public static void main(String[] args)
      {
      int[] l={10,20,30};
      int[] m={40,50};
      methodOne(l,m);
      }
      }

      Delete

Post a Comment

Popular posts from this blog

Core Java

Content 1 - Language fundamental   Identifier Reserved words Data types Literals Arrays Types of variables Var arg method Main method Java coding standards 2 -  Declaration and Access Modifiers Java source file structure Class modifiers Member modifiers Interfaces 3 - Exception Handling Introduction Runtime stack mechanism Default exception handling in java Exception hierarchy Customized exception handling by try-catch  Control flow in try-catch Methods to print exception information Try with multiple catch blocks Finally Difference between final, finally, finalize Control flow in try-catch-finally Control flow in nested try-catch-finally Various possible combinations of try-catch-finally throw keyword throws keyword Exception handling keywords summary Various possible compile-time errors in exception handling Customized exceptions Top-10 exceptions  4 - String Concept of String Immutable String String Comparison String Conca