Hello!! I am Aung Khant Kyaw. Previous time I posted about - TopicsExpress



          

Hello!! I am Aung Khant Kyaw. Previous time I posted about reflection method in java.So I think you will familier with how to use reflection. Today I would like to post about Varargs(Variable argument). If you declare a method with one argument or two arguments or three arguments,could you use this method for four parameter? I will explain with example. For example, public void Sum(int a,int b){....} For above method,if you want to call that method, you can call from other method like this: sum(1,2); But you cant call this method for less than one parameter and more that two parameter.If you want to use for adding integer with different number of parameter,you need to create another sum method with different name.So there will be many method and it will be waste. By using Varargs,this problem solved automatically. The syntax for Vararges is adding three dots between Data type and variable name in the argument.Syntax is sum(int...a){....} By using three dots, it work as Array.So you can add parameter dynamically.Please look follwing sample. ------------------------------------------------------------- class VarArgsIntExample { public static void main(String args[]) { sum(1,2,4,5); sum(1,2); } public static void sum(int... number) { int sum=0; for(int index=0;index
Posted on: Tue, 13 Jan 2015 10:22:53 +0000

Trending Topics



Recently Viewed Topics




© 2015