File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes invoking methods - help!!!!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "invoking methods - help!!!!!" Watch "invoking methods - help!!!!!" New topic
Author

invoking methods - help!!!!!

Rob Petterson
Ranch Hand

Joined: Jan 23, 2002
Posts: 149
What happens when you compile and run the following code?
public class Test
{
public void myMethod(Object o)
{
system.out.println("My Object");
}
public void myMethod(String s)
{
system.out.println("My String");
}
public static void main(String args[])
{
Test t = new Test();
t.myMethod(null);
}
}
// Why does it print "My String"??
// Is it because it is the more specific method in
// the hierachy tree??


Rob Petterson
SCJP
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
// Why does it print "My String"??
// Is it because it is the more specific method in
// the hierachy tree??

That's exactly that !
You might want to have a look at JLS 15.12.2 Compile-Time Step 2: Determine Method Signature
and
JLS 15.12 Method Invocation Expression in Plain English


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Yes String parameter is more specific than Object.
Read about the whole process at JLS 15.12


SCJP2. Please Indent your code using UBB Code
Rob Petterson
Ranch Hand

Joined: Jan 23, 2002
Posts: 149
Thanks for that. The JLS explains it well - cheers
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5370
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=012648
HTH


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: invoking methods - help!!!!!
 
Similar Threads
Call to overloaded method with null reference
Method resolution
no ambiguous reference error for Object?
regarding StackOverflowError
overloading