This week's book giveaway is in the Open Source Projects forum.
We're giving away four copies of Eclipse Collections Categorically: Level up your programming game and have Donald Raab on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

why this outout

 
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,

Why the following code is not giving compile time error


Thanks
scjp1.4
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Lakshmi,

Why do you think that it should give a compile time error?? The method method(null) matches the string version and prints string as o/p. If you remove the static method that takes String as argument, it prints object as the O/P. It's a good question. Can anyone please explain this behaviour.

Regards,
Jothi Shankar Kumar. S
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

The null Literal can be cast into any type of Object Reference.
So any one of the given method()'s can be called but the compiler tries to choose the most specific one.

Now the null matches the call for method(Object )
then it also matches the call for method(String )

since the string version is more specific the call is made to the method(String ) version.

But if you overload the method with Integer
metod(Integer ) then the complier does not know which one to call because
The String version is as specific as Integer version so the compiler shouts that the call is "AMBIGUOUS"
 
Enthuware Software Support
Posts: 4910
60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can almost hear someone say, "how do you know X is more specific than Y?"

Simple answer is, if X extends Y directly or indirectly, X is more specfic than Y. If there is no direct or indirect inheritance relationship between X and Y, both are equally specific.

So Integer is more specific than Number and Number is more specific than Object. But String and Integer(or Number) are equally specific.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,


this ambiguity can be seen for interfaces as well:





This code produces:
class A


but if you delete the method // for A's
it is ambigous and the line foo(a); in main cannot compile.
Same would be true for foo(null);




Yours,
Bu.
 
Willie Smits increased rainfall 25% in three years by planting trees. Tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic