Page 2 of 2

Re: About Question enthuware.ocajp.i.v7.2.1100 :

Posted: Sat May 25, 2019 2:52 am
by zel_bl

Re: About Question enthuware.ocajp.i.v7.2.1100 :

Posted: Thu Feb 02, 2023 3:40 pm
by asi-aal
void probe(long x) { System.out.println("In long"); } //3

void probe(Long x) { System.out.println("In LONG"); } //4

public static void main(String[] args){
Integer a = 4; new TestClass().probe(a); //5
int b = 4; new TestClass().probe(b); //6
}

Both will result "in long" can you explain me the idea behind the first one "Integer a = 4; new TestClass().probe(a)" ?

Re: About Question enthuware.ocajp.i.v7.2.1100 :

Posted: Fri Feb 03, 2023 12:54 am
by admin
Integer is not a subclass of Long, so probe(a) cannot be bound to probe(Long x).

But an Integer can be unboxed to an int and int can be promoted to long (not Long). Therefore, probe(long x) will be invoked.

You might want to go through a book to understand how method selection works in case of overloading.

Section 10.2.3 of this book explains it very clearly: https://amzn.to/2PucBeT