Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Function Overloading

Tags:

java

Can anyone tell me, why does "int" gets printed, as in the internal working.

class Test {

void Method(int i) {
    System.out.println("int");
}
void Method(String n) {
    System.out.println("string");
}

public static void main(String [] a) {
    new Test().Method('c');
    }

}
like image 767
ncst Avatar asked Dec 16 '22 08:12

ncst


1 Answers

The character c is being cast to an int. It isn't going to be cast to a String.

More formally, from the JLS §5.5 - a widening conversion takes place from a char to an int, long, float, or double.

The reason that will never be seen as a String is that a char can't be autoboxed into a String.

like image 74
Makoto Avatar answered Jan 01 '23 05:01

Makoto