Farruh Habibullaev Farruh Habibullaev - 4 months ago 11
Java Question

Java overloading - why are int and Integer the same?

Why does JAVA see Integer object and int primitive type the same when overloading for example:

ClassOne {

void myMethod(int number){

}

void myMethod(Integer number) {

}
}

Answer

It does not see them as the same, it just treats them the same. If you want to explicitly use an Integer you'll have to explicitly cast it.

@Test
public void testOverload() {
    test123(123); //Logs "int-Method"
    test123((Integer)123); //Logs "Integer-Method"
}

private void test123(int ix) {
    LOG.debug("int-Method");
}

private void test123(Integer ix) {
    LOG.debug("Integer-Method");
}
Comments