About Question enthuware.ocajp.i.v7.2.1100 :
Moderator: admin
-
- Posts: 22
- Joined: Sun Nov 18, 2012 1:51 am
- Location: Moscow
- Contact:
About Question enthuware.ocajp.i.v7.2.1100 :
void probe(int... x) { System.out.println("In ..."); } //1
Sorry, I can't find anything about this construction: int and 3 dots.
What is it?
Sorry, I can't find anything about this construction: int and 3 dots.
What is it?
-
- Site Admin
- Posts: 9802
- 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: 2
- Joined: Tue Aug 27, 2013 11:54 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1100 :
Hello.
Is boxing/unboxing really in scope of 1Z0-803 exam?
Thanks,
Michal
Is boxing/unboxing really in scope of 1Z0-803 exam?
Thanks,
Michal
-
- Site Admin
- Posts: 9802
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1100 :
They haven't mentioned it explicity but some candidates have reported getting questions on this aspect.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 18
- Joined: Mon Jan 13, 2014 5:39 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1100 :
I tried actually running this and yeah it uses the Integer and long probe methods, but I guess I don't understand why the second case is using the probe(long) method. Wouldn't the probe(int...) be the closer match?
-
- Posts: 37
- Joined: Thu Feb 13, 2014 12:58 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1100 :
I used (int x) instead of (int... x) the result was "In Integer & In ... "
which means if we don't use Var-args, auto-boxing preferred over widening.
this sentence needs more explanation:
The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments.
which means if we don't use Var-args, auto-boxing preferred over widening.
this sentence needs more explanation:
The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments.
-
- Site Admin
- Posts: 9802
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1100 :
This should be helpful for getting the basics on var-args: https://today.java.net/pub/a/today/2004 ... rargs.htmlfasty23 wrote:I used (int x) instead of (int... x) the result was "In Integer & In ... "
which means if we don't use Var-args, auto-boxing preferred over widening.
this sentence needs more explanation:
The three periods after the final parameter's type indicate that the final argument may be passed as an array or as a sequence of arguments.
If you like our products and services, please help us by posting your review here.
-
- Posts: 36
- Joined: Tue May 06, 2014 8:30 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1100 :
I understand the rules stated in the explanation; however, I do not understand why calling the method probe(int... i) with probe(int) is considered boxing/unboxing. My understanding is that varargs and very similar to arrays. I see probe(int... i) is equivalent to probe(int[] i)?
Is it not? Please explain. Would you explain why calling the varargs is considered boxing/unboxing?
Is it not? Please explain. Would you explain why calling the varargs is considered boxing/unboxing?
-
- Site Admin
- Posts: 9802
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1100 :
I don't think the explanation claims what you are suggesting. Can you please quote the statement that leads you to think that way?
It is just trying to explain the general thought process behind these bindings. Calling probe(int... i) with probe(int) does not involve boxing/unboxing but calling it with probe(Integer) does.
HTH,
Paul.
It is just trying to explain the general thought process behind these bindings. Calling probe(int... i) with probe(int) does not involve boxing/unboxing but calling it with probe(Integer) does.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 36
- Joined: Tue May 06, 2014 8:30 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1100 :
excerpt from the explanation:
2. probe(int) is bound to probe(long) (because of Rule 2) , then to probe(Integer ) because boxing an int qives you an Integer, which matches exactly to probe(Integer), and then to probe(int...).
However, in your rule (3rd step) I now see you stated more clearly. Please let me know if my understanding is correct.
widening is preferred over boxing/unboxing
boxing/unboxing is preferred over varargs
Is the statement above correct?
2. probe(int) is bound to probe(long) (because of Rule 2) , then to probe(Integer ) because boxing an int qives you an Integer, which matches exactly to probe(Integer), and then to probe(int...).
However, in your rule (3rd step) I now see you stated more clearly. Please let me know if my understanding is correct.
widening is preferred over boxing/unboxing
boxing/unboxing is preferred over varargs
Is the statement above correct?
-
- Site Admin
- Posts: 9802
- 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: 9802
- 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: 9802
- 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: 9802
- 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: 9802
- 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: 9802
- 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: 9802
- 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: 9802
- 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.
Who is online
Users browsing this forum: No registered users and 3 guests