• Post Reply Bookmark Topic Watch Topic
  • New Topic

(MyClass) null  RSS feed

 
Ranch Hand
Posts: 1855
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it that meaningless enough or not . You say.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not meaningless. Consider the following:



Which method gets called by TheClass.doMethod(null)?
 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi


when i execute this program, it gives output, String.

But, when i add one more method,


This gives a compilation error, "The method method(Object) is ambigious for the type Sample".

why this is happening?
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why this is happening?



See this FAQ (or at least, part of)...

http://faq.javaranch.com/java/ScjpFaq#mostSpecific

Henry
 
Anoobkumar Padmanabhan
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong,

But my question is that why null is associated with String? It is common to Object. Right?
Also, when i add the method with Integer argument, the error message was, " ... method(Object) is ambigious ...". That means null had taken as Object.

i am so confused in this. Please explain.
 
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anoobkumar Padmanabhan:
... But my question is that why null is associated with String? It is common to Object...

Did you read the last line of the FAQ entry Henry linked to?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But my question is that why null is associated with String? It is common to Object. Right?


The FAQ explains this...

Also, when i add the method with Integer argument, the error message was, " ... method(Object) is ambigious ...". That means null had taken as Object.


The FAQ explains this too...


Now, if you don't agree, can you elaborate why do you think that the FAQ doesn't explain it, so that I can elaborate on the explanation.

Henry
 
Anoobkumar Padmanabhan
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In general, one method is considered more specific than another if its argument types are subtypes of the other method's respective argument types


If it's not possible to identify a most-specific method from among the applicable methods, then the invocation is considered "ambiguous" and results in a compile-time error.


I got the answer from these two sentences thank you all for giving me the tips.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!