• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Method invocation with null argument

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all
This code is small part of Dan's Questions:

This code prints "String". Why?
So how we can know that which overloaded method will be invoked when the argument is null.
Thanx in advance
Alexan
[ July 01, 2003: Message edited by: Alexan Kahkejian ]
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The most specific method is invoked. In this case, the method taking a String argument is more specific than a method taking an Object argument.
Please see the following posts which deal with the same topic:
https://coderanch.com/t/239328/java-programmer-SCJP/certification/null-argument-method-being-passed
https://coderanch.com/t/239534/java-programmer-SCJP/certification/null-arg-overloaded-method
Also check out JLS 15.12.2.2 Choose the Most Specific Method to find out about how methods are resolved.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The String class is a subclass of the object. When the method m is called with null, the subclass gets called. I guess this could be a general rule that a subclass gets called before superclass.
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I do, and I hope this is basically the right approach, is look at type of the argument/s and ask myself which one can be implictly cast to all the others. If there is a tie between two or more, then it is a compile error (ambiguous).
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic