• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

K&B Mock Question 5

 
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can anyone explain why is the result of the following code is H) 4, 3


What is the result?

A). 1 1
B). 2 1
C). 3 1
D). 4 1
E). 1 3
F). 2 3
G). 3 3
H). 4 3

Why is Number preferred instead of Integer or Long in the case of System.out.print(doX(s,s) + " ");

Thanks
 
Ranch Hand
Posts: 1609
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting! !! but i would also like to be cleared on...... how the output differs both on SCJP1.4 and and scjp1.5...
 
Sandeep Chhabra
Ranch Hand
Posts: 340
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Akhil...it wont even compile in JDK 1.4 (not SCJP1.4).
Try it!!
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following rules apply (in order):
1) Look for the most closely matching method without VarArg, auto-boxing/unboxing, if not, then
2) Most closely Matching method without VarArg but with auto-boxing/unboxing, still not, then
3) Most closely Matching method with VarArg and with Auto-boxing/unboxing

For

No matching method is found for (1). So (2) applies. Following method is most closely matching method, so it selected

For
, same rule applies, (2). So following is selected

So output is

Oh forgot,

Why is Number preferred instead of Integer or Long in the case of System.out.print(doX(s,s) + " ");

Apply OOP concept and you will find the answer. Hint: Number is superclass of Integer, Long, etc..

For example,


Output:
Above rule applies along with OOP.
[ September 27, 2005: Message edited by: Tushar Patel ]
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3) Most closely Matching method with VarArg and with Auto-boxing/unboxing

Can it be decoupled like this ?
3a) Most closely Matching method with VarArg and without Auto-boxing/unboxing
3b) Most closely Matching method with VarArg and with Auto-boxing/unboxing

Because in your last example (display 2), I fail to see any autoboxing / unboxing
[ September 27, 2005: Message edited by: Cyrano DeBergerac ]
 
Tushar Patel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Cyrano DeBergerac:
3) Most closely Matching method with VarArg and with Auto-boxing/unboxing

Can it be decoupled like this ?
3a) Most closely Matching method with VarArg and without Auto-boxing/unboxing
3b) Most closely Matching method with VarArg and with Auto-boxing/unboxing

Because in your last example (display 2), I fail to see any autoboxing / unboxing



Kind of, but you have to be very careful if you have both else you will get ambiguity. For example,

I think, you can have most closely mactching method using 3a) OR 3b), but not both as shown in example. So I think, 3) can be like this:
3) Most closely Matching method with VarArg and/or with Auto-boxing/unboxing

Please correct me. It is kind of interesting!!

Thanks Cyrano for your input.
 
Eric Janssens
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By John Smith ( previously known as Cyrano DeBergerac )

After few testing, i found out that the only presence of ... change completely the way to found the corresponding method:

void doX(int i) { ... );
void doX(Integer i) { ... };

doX(5);
doX(new Integer(5)); evoke their corresponding methods ( best match )

BUT

void doX(int i, String... strings) { ... };
void doX(Integer i, String... strings) { ... };

doX(5);
doX(new Integer(5)); compile time error for both

conclusion:
3) Find the ONLY POSSIBLE matching method with VarArg and any possible Auto-boxing / Unboxing. Compile-time error if more than one method is possible.


So in case like:
doX(long... longs) { ... };
doX(Number... numbers) { ... };

any possible call of long is include in Number... numbers, so any call with a list of parameters which can be transform in long... via conversion and AutoBoxing/Unboxing will throw a compiletime error.

For me, Java should launch a compile time error in the creation of the methods when one args list with varargs is completely include in the other or change their way to found the corresponding method with varargs.

Tell me if I made any mistake
[ September 27, 2005: Message edited by: John Smith ]
 
Tushar Patel
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's right John Smith(at the time of posting)!!

Therefore you cannot have both matching methods for 3a) AND 3b). You can have only one, one or the other, 3a) OR 3b).

Take Care,
[ September 27, 2005: Message edited by: Tushar Patel ]
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey John -

I can't quite make out what your last post is saying If you give it another try I'll let you know what I think... there's something about what you're saying that seems wrong, but since I can't quite understand your examples I'm not sure...

- Bert
 
Eric Janssens
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thx to take care of us. Here the complete codes:

normal case: ( nothing new here )



As soon as u introduce the varargs, u cannot have multiple matching:



this lead to the 3rd case where some method are completely useless when the signature is completely hide by an other method. This case should send some compile time error at the declaration of methods. ( in my opinion ) or the algo to find the correct overloading method should be change.


[ September 27, 2005: Message edited by: John Smith ]
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John,

As soon as u introduce the varargs, u cannot have multiple matching



I'm not sure what you're trying to do here, you haven't defined any methods that take just an int or just an integer? your invocations of doX are wrong?

- Bert
 
Eric Janssens
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1) Look for the most closely matching method without VarArg, auto-boxing/unboxing, if not, then
2) Most closely Matching method without VarArg but with auto-boxing/unboxing, still not, then
3) Find the unique Matching method with VarArg with or without Auto-boxing/unboxing OR compiletime error

the first code was just to show how it works without varargs ( step1 )
the second and third piece of code was about the step3 and how the JVM find the proper method if it has failed to find it with the step1 and 2.

If I put the perfect match in the second and third piece of code, i ll not be able to demonstrate how the JVM find the method on step3.

I am not sure about the meaning of your last post. I have define a way to access with only one int or one Integer + any number of String including 0.
In fact, the main problem is that I had define 2 of them
[ September 27, 2005: Message edited by: John Smith ]
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A-ha John,

Got it!

You're right about how this works, the good news is that this subtlety isn't on the exam!

Good catch!

- Bert
 
Eric Janssens
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bert Bates:
the good news is that this subtlety isn't on the exam!



Hopefully, or I dont know how many years it will take to me to be ready
[ September 28, 2005: Message edited by: John Smith ]
 
Bert Bates
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I promise!
 
He puts the "turd" in "saturday". Speaking of which, have you smelled this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic