Friday, April 12, 2019

variable argument with overloading ambiguity in java

// Java program to demonstrate varargs
public class cl
{
// A method that takes variable number of intger
// arguments.


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

static void f(int 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();
f(1.1);// no parameter
}
}

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

overloaded function
1.1

No comments:

Post a Comment