Posts

Showing posts from November, 2014

Java Variable Arguments

Java variable arguments (varargs) was introduced in java 1.5.usage of varargs is  if u want to pass more arguments to the method say example more than 3 or 4 of same data type then we can go for varargs which makes the code simple and neat. Before varargs developer used two options creating a collection like list or set and passing all the data with that overloading  method with different parameter  But we have some disadvantages  in the methods mentioned above lets  take method overloading private int varargsDemo(int i, int j) {         int result =i*j;         return result;     }        private int varargsDemo(int i, int j,int k) {         int result =i*j*k;         return result;     } Here you can see if you use overloading your code becomes little clumsy and you need to create more number of overloading methods also the main thing is you should know the max number of arguments you want to pass before your write the code. now consider collections     pr