GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Why call to method with null invokes String Parameter and not Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why call to method with null invokes String Parameter and not Object" Watch "Why call to method with null invokes String Parameter and not Object" New topic
Author

Why call to method with null invokes String Parameter and not Object

Sunny Kumar Kalra
Greenhorn

Joined: Oct 10, 2010
Posts: 15
Hi All,

I have a question in mind :

anyObject.callme(NULL)

callme (String)
callme (Object)

--> invokes callme(String ) and not callme(Object) , Why so ?
Michael Petrovich
Greenhorn

Joined: Sep 06, 2011
Posts: 16

The informal intuition is that one method is more specific than another if any invocation handled by the first method could be passed on to the other one without a compile-time type error.


In your case you can invoke callme (Object) both with new Object() and new String() parameters without Compile Time Errors. But you can't invoke callme (String) with new Object() without Compile Time Error so the callme (String) is more specific.
For more academic details see http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html#15.12.2.5
 
GeeCON Prague 2014
 
subject: Why call to method with null invokes String Parameter and not Object