Page 1 of 2

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

Posted: Sun Nov 18, 2012 8:09 am
by Svetopolk
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?

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

Posted: Sun Nov 18, 2012 8:23 am
by admin
This is called varargs.

HTH,
Paul.

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

Posted: Wed Aug 28, 2013 6:23 am
by loozak84
Hello.
Is boxing/unboxing really in scope of 1Z0-803 exam?

Thanks,
Michal

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

Posted: Wed Aug 28, 2013 8:37 am
by admin
They haven't mentioned it explicity but some candidates have reported getting questions on this aspect.

HTH,
Paul.

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

Posted: Tue Jan 14, 2014 3:48 am
by kecker
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?

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

Posted: Thu Feb 13, 2014 5:11 pm
by fasty23
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.

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

Posted: Thu Feb 13, 2014 9:08 pm
by admin
fasty23 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.
This should be helpful for getting the basics on var-args: https://today.java.net/pub/a/today/2004 ... rargs.html

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

Posted: Tue May 06, 2014 8:36 am
by vchhang
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?

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

Posted: Tue May 06, 2014 10:21 am
by admin
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.

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

Posted: Tue May 06, 2014 10:44 am
by vchhang
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?

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

Posted: Tue May 06, 2014 10:58 am
by admin
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.

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

Posted: Tue Oct 17, 2017 6:05 pm
by Sergey
probe(long) is preferred over probe(int...) because unboxing an Integer gives an int and in pre 1.5 code
What does this phrase mean? I am from china, i know only chinese english.

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

Posted: Tue Oct 17, 2017 11:47 pm
by admin
It means code that is written for the versions of Java older than 1.5.

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

Posted: Sat Dec 08, 2018 5:30 pm
by flex567
Does this rule apply:

Code: Select all

Consider widening before varargs
if you have 2 methods - probe(int... ) and probe(long), to which one will probe(int) be bound?

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

Posted: Sat Dec 08, 2018 10:05 pm
by admin
Yes, it applies. "Consider widening before varargs" rule is there because of backward compatibility with pre 1.5.

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

Posted: Sun Dec 09, 2018 6:12 am
by flex567
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?

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

Posted: Sun Dec 09, 2018 6:31 am
by admin
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.

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

Posted: Sun Dec 09, 2018 8:32 am
by flex567
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.

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

Posted: Sun Dec 09, 2018 8:59 am
by admin
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?

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

Posted: Sun Dec 09, 2018 10:54 am
by flex567
Then it would go to probe(int ).

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

Posted: Sun Dec 09, 2018 10:59 am
by admin
:thumbup:

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

Posted: Thu May 23, 2019 3:36 am
by zel_bl
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

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

Posted: Thu May 23, 2019 4:07 am
by admin
>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.

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

Posted: Thu May 23, 2019 4:56 am
by zel_bl

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
From the Boyarsky, Selikoff book: ...... Java is happy to convert the int 4 to a long 4 or an Integer 4. It cannot handle converting in two steps to a long and then to a Long. .......

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
Here Integer a is first unboxed into an int and then widened to a long. I see it it as a two step conversion and it works, so I wonder if there is a rule about two step conversion for primitives (when it doesn't work) and for Object references (Integer ... ) when it works.

Thanks

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

Posted: Thu May 23, 2019 10:45 am
by admin
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.