It is a basic question, but I couldn't get a the proper answer. I am thinking it is because of primitive types auto type casting
Why would the below statement invokes the print(int x) method and not print (char x) method.
public class Overloading {
public static void main(String args[])
{
byte b='x';
print(b);
}
public static void print(int x)
{
System.out.println("Inside int Print "+x);
}
public static void print(char x)
{
System.out.println("Inside char Print "+x);
}
public static void print(float x)
{
System.out.println("Inside float Print "+x);
}
}
The conversion that may be used for this method invocation conversion is a widening primitive conversion :
byte to short, int, long, float, or double
short to int, long, float, or double
char to int, long, float, or double
int to long, float, or double
long to float or double
float to double
You see there is no byte
to char
path. This isn't a priority matter : if you remove the function taking an int
as parameter, your code won't compile.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With