Friday, April 12, 2019

variable argument with overloading in java




// Java program to demonstrate varargs
public class cl
{



static void f(double x)
{
    System.out.println("overloaded function");
    System.out.println(x);
}



static  void f(int ...a)
{
System.out.println("Number of arguments: " + a.length);


for (int i=0;i<a.length;i++)
System.out.print(i + " ");
System.out.println();
}





public static void main(String args[])
{

f(100); // one parameter
f(1, 2, 3, 4); // four parameters
f(); // no parameter
f(1.1);
}
}


output:
$javac cl.java
$java -Xmx128M -Xms16M cl
Number of arguments: 1
0 
Number of arguments: 4
0 1 2 3 
Number of arguments: 0

overloaded function
1.1

No comments:

Post a Comment