About Question enthuware.ocajp.i.v7.2.1100 :
Moderator: admin
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1100 :
Yes, it is correct. That is why if you have only two methods - probe(long) and probe(Integer), probe(int) will be bound to probe(long) instead of probe(Integer). Similarly, if you have only two methods - probe(int... ) and probe(Integer), probe(int) will be bound to probe(Integer ) instead of probe(int...).
If you just have one method probe(int...), probe(Integer) can be bound to it. So that does link var args and boxing/unboxing.
HTH,
Paul.
If you just have one method probe(int...), probe(Integer) can be bound to it. So that does link var args and boxing/unboxing.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 39
- Joined: Sat Jul 29, 2017 1:04 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1100 :
What does this phrase mean? I am from china, i know only chinese english.probe(long) is preferred over probe(int...) because unboxing an Integer gives an int and in pre 1.5 code
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1100 :
It means code that is written for the versions of Java older than 1.5.
If you like our products and services, please help us by posting your review here.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1100 :
Does this rule apply:
if you have 2 methods - probe(int... ) and probe(long), to which one will probe(int) be bound?
Code: Select all
Consider widening before varargs
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1100 :
Yes, it applies. "Consider widening before varargs" rule is there because of backward compatibility with pre 1.5.
If you like our products and services, please help us by posting your review here.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1100 :
Which one is has higher prio
- Consider widening before varargs
- Consider widening before autoboxing
If you have 2 methods - probe(int... ) and probe(Integer), to which one will probe(int) be bound?
- Consider widening before varargs
- Consider widening before autoboxing
If you have 2 methods - probe(int... ) and probe(Integer), to which one will probe(int) be bound?
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1100 :
Widening would be irrelevant in this case, because int does not require widening to an Integer or int...you can autobox an int into an Integer. Thus, in this case, probe(Integer ) will be used instead of probe(int... )
You need to go through Section 8.2.3 of Hanumant's book. It explains the rules quite clearly. On page 188, it says:
4. Consider widening before autoboxing
and
5. Consider autoboxing before varargs
Therefore: widening > autoboxing > varargs in terms of priority.
Now, can you tell which one will be used if you try to call probe with a short? i.e. short s = 10; probe(s)? Read the above section if you can't answer this.
You need to go through Section 8.2.3 of Hanumant's book. It explains the rules quite clearly. On page 188, it says:
4. Consider widening before autoboxing
and
5. Consider autoboxing before varargs
Therefore: widening > autoboxing > varargs in terms of priority.
Now, can you tell which one will be used if you try to call probe with a short? i.e. short s = 10; probe(s)? Read the above section if you can't answer this.
If you like our products and services, please help us by posting your review here.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1100 :
I don't know what is the exact question but I think it is
If you have 2 methods - probe(int... ) and probe(Integer), to which one will probe(short) be bound?
This is very similar example from the book (location 4573).
so the probe(int...) will be used.
If you have 2 methods - probe(int... ) and probe(Integer), to which one will probe(short) be bound?
This is very similar example from the book (location 4573).
so the probe(int...) will be used.
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1100 :
Correct because in this case short can neither be boxed into an Integer nor be widened to Integer. So the only option remaining is varargs. But if you also had a method probe(int ) in addition to probe(Integer ) and probe(int...), then?
If you like our products and services, please help us by posting your review here.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1100 :
Then it would go to probe(int ).
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1100 :
If you like our products and services, please help us by posting your review here.
-
- Posts: 10
- Joined: Mon Mar 04, 2019 3:42 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1100 :
I have doubts about double conversion. As I recall double conversion isn't allowed in such cases (most specific parameter). But if we have a method invocation with an argument of type Integer and only option is a method with a long primitive, Integer will be unboxed to an int and widened to a long parameter. Is it true that an object reference may be unboxed and widened. On the other hand, a primitive can not be widened and boxed, or boxed and widened together.
I'd appreciate If you could explain when and what is allowed or provide a link on the subject.
Thanks
I'd appreciate If you could explain when and what is allowed or provide a link on the subject.
Thanks
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1100 :
>I have doubts about double conversion. As I recall double conversion isn't allowed in such cases (most specific parameter).
Please post exact code that you have a doubt about. Also post the result that you get after compilation and/or execution so that your doubt can be clearly understood.
>Is it true that an object reference may be unboxed and widened.
You could try a very simple code to see - Integer i = 10; long ln = i; Compile it and see what happens.
This is a fairly large topic to be explained in a post. You may either go through Chapter 5 of the JLS or through any other book.
Please post exact code that you have a doubt about. Also post the result that you get after compilation and/or execution so that your doubt can be clearly understood.
>Is it true that an object reference may be unboxed and widened.
You could try a very simple code to see - Integer i = 10; long ln = i; Compile it and see what happens.
This is a fairly large topic to be explained in a post. You may either go through Chapter 5 of the JLS or through any other book.
If you like our products and services, please help us by posting your review here.
-
- Posts: 10
- Joined: Mon Mar 04, 2019 3:42 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1100 :
Code: Select all
public class TooManyConversions {
public static void play(Long l){}
public static void (Long... l){}
public static void main(String[] args) {
play(4); // DOES NOT COMPILE
play(4L); // cals the Long version
Code: Select all
void probe(long x) {
System.out.println("In long");
}
public static void main(String[] args) {
Integer a = 4;
new TestClass().probe(a); // result: In long
Thanks
-
- Site Admin
- Posts: 10036
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1100 :
Not sure in what context that statement is made but the author is usually available on CodeRanch.com java certification forum and is quite helpful. You might want to ask for a clarification there.
If you like our products and services, please help us by posting your review here.
-
- 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: 10036
- 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 56 guests