This text helped
http://javabypatel.blogspot.com/2016/05 ... ading.html
About Question enthuware.ocajp.i.v7.2.1100 :
Moderator: admin
-
- Posts: 10
- Joined: Mon Mar 04, 2019 3:42 am
- Contact:
-
- Posts: 10
- Joined: Wed Nov 23, 2022 3:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1100 :
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)" ?
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)" ?
-
- Site Admin
- Posts: 9802
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1100 :
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
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.
Who is online
Users browsing this forum: No registered users and 2 guests