Win a copy of Practice Tests for OCP Java 17 Certification Exam (1Z0-829) this week in the OCPJP forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Rob Spoor
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Scott Selikoff
Bartenders:
  • Piet Souris
  • Jj Roberts
  • fred rosenberger

calling static method ,self-test question no.15(JAVA 6 CERTIFICATION Guide-310-065)

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



SCJP1.6 JAVA 6 CERTIFICATION Guide-310-065
by Katherine Sierra (Author), Bert Bates (Author)

pg-169
self test







What is the result?
A. -124
B. -134
C. -424
D. -434
E. -444
F. Compilation fails


ans: D is correct. In general, overloaded var-args methods are chosen last. Remember that arrays
are objects. Finally, an int can be boxed to an Integer and then "widened" to an Object


i still didn't get it.please explain
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Method signature matching can get a bit confusing. Primitive widening
comes first, and then classes including boxing, which can be widened
to Object. Looking for a varargs match comes last.

So the first call is widened to Object and finds a match there, before it
gets to vararg, where it would also match. The second call is a direct
hit. In the third call, the int widens all the way to double, but finds no
match. It is then boxed - again no match. Finally Integer is widened
and hits on Object. I hope this helps.

Jim ... ...
 
Mohit G Gupta
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why will there be a direct hit in case of
second call ?

 
Sheriff
Posts: 9693
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

mohitkumar gupta wrote:why will there be a direct hit in case of second call ?


I cannot understand your question properly, but the second call is not that hard to understand. You called sifter with an B[] and there was a sifter method taking B[] as parameter so it got called. The B[]... version of sifter didn't get called because it was not a better match...
 
Ranch Hand
Posts: 30
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the first case for shifter(aa) line 17 is not possible coz b is a sub class of a so it is widened to object and line 18 is executed
2nd case for shifter(ba) simply line 17 is called
3rd case for shifter(7), 7 is autoboxed to Integer and then widened to Object so line 18 is executed.
Always remember "Only consider the var-arg parameter methods when no Non-var-arg methods are possible"

I hope you understand it clearly.. any more queries???
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot everyone...
even I had doubt in this question and now i hv understood this..
 
Sandy Ghosh
Ranch Hand
Posts: 30
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
welcome..
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic