Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Program of Autoboxing where boxing beats varargs.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 244
    Comment on it

    varargs

    varangs is variable arguments. varangs as name suggest allows a method to have 0 or mutiple argument.

    Autoboxing

    Autoboxing is converting primitive data type into it's equivalent wrapper type class automatically in Java.E.g: int primitive type is converted to Integer Wrapper class in autoboxing.

    Program of Autoboxing where boxing beats varargs:

    class Boxing3
    {  
      static void m(Integer i)  //overloaded method with wrapper type as parameter
      { 
        System.out.println("Integer");
      }  
        static void m(Integer... i) //overloaded method with varargs
      {
        System.out.println("Integer...");
      }  
      
      public static void main(String args[])
      {  
       int a=30;  
       m(a);  
     }   
    }  

    Output:

    Integer

    In above program when overloaded method m is called from main with int primitive type value as parameter the function m with wrapper type Integer is called because there is not method with int primitive type but method with Integer wrapper type is available therefore int is automatically converted to Integer i.e boxing is performed. Boxing is given preference over varargs i.e m method with varargs is not called.

     

 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: