• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

invoking methods - help!!!!!

 
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// 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
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes String parameter is more specific than Object.
Read about the whole process at JLS 15.12
 
Rob Petterson
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that. The JLS explains it well - cheers
 
Ranch Hand
Posts: 5397
1
Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=24&t=012648
HTH
 
My honeysuckle is blooming this year! Now to fertilize this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic