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

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.


asi-aal
Posts: 10
Joined: Wed Nov 23, 2022 3:40 am
Contact:

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

Post by asi-aal »

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)" ?

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 »

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
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 52 guests