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

Svetopolk
Posts: 22
Joined: Sun Nov 18, 2012 1:51 am
Location: Moscow
Contact:

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

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

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 »

This is called varargs.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

loozak84
Posts: 2
Joined: Tue Aug 27, 2013 11:54 am
Contact:

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

Post by loozak84 »

Hello.
Is boxing/unboxing really in scope of 1Z0-803 exam?

Thanks,
Michal

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 »

They haven't mentioned it explicity but some candidates have reported getting questions on this aspect.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

kecker
Posts: 18
Joined: Mon Jan 13, 2014 5:39 am
Contact:

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

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

fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

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

Post 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.

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 »

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

vchhang
Posts: 36
Joined: Tue May 06, 2014 8:30 am
Contact:

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

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

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 »

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

vchhang
Posts: 36
Joined: Tue May 06, 2014 8:30 am
Contact:

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

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

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 »

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

Sergey
Posts: 39
Joined: Sat Jul 29, 2017 1:04 pm
Contact:

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

Post 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.

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 »

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.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

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

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 »

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.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

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

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 »

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

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post 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.

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 »

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.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 »

Then it would go to probe(int ).

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 »

:thumbup:
If you like our products and services, please help us by posting your review here.

zel_bl
Posts: 10
Joined: Mon Mar 04, 2019 3:42 am
Contact:

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

Post 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

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 »

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

zel_bl
Posts: 10
Joined: Mon Mar 04, 2019 3:42 am
Contact:

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

Post 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

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 »

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 22 guests