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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin


asi-aal
Posts: 10
Joined: Wed Nov 23, 2022 3:40 am
Contact:

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

Post 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)" ?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 30 guests