Java uses the type difference to determine which overloaded method to
call. If one method signature is strictly more specific than the other,
then Java chooses it without an error.
Example :-
//Java program to illustrate
//method overloading in varargs
public class varargsDemo
{
public static void main(String[] args)
{
fun();
}
//varargs method with float datatype
static void fun(float... x)
{
System.out.println("float varargs");
}
//varargs method with int datatype
static void fun(int... x)
{
System.out.println("int varargs");
}
//varargs method with double datatype
static void fun(double... x)
{
System.out.println("double varargs");
}
}
Output:
Example :-
//Java program to illustrate
//method overloading in varargs
public class varargsDemo
{
public static void main(String[] args)
{
fun();
}
//varargs method with float datatype
static void fun(float... x)
{
System.out.println("float varargs");
}
//varargs method with int datatype
static void fun(int... x)
{
System.out.println("int varargs");
}
//varargs method with double datatype
static void fun(double... x)
{
System.out.println("double varargs");
}
}
Output:
int varargs
The rule that the most specific method is chosen according to type promotion.
- double > float
- float > long
- long > int
- int > char
- int > short
- short > byte
No comments:
Post a Comment