• Post Reply Bookmark Topic Watch Topic
  • New Topic

When we declare null in m1 method Test arg is coming as output How it is posiible ?  RSS feed

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
According to the JLS:

Determining which Method Signature to use is a Compile-Time event, which (among others) follows this rule:
"The second step searches the type determined in the previous step for member methods. This step uses the name of the method and the types of the argument expressions to locate methods that are both accessible and applicable, that is, declarations that can be correctly invoked on the given arguments. There may be more than one such method, in which case the most specific one is chosen."

In your case you have two applicable method signatures. Since null is type-less is can work for either method signature and therefore the more specific one is chosen. Since Test is a sub-class of Object, it is more specific than Object and is the method linked to the method-call expression.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I understand your question correctly, it's because PrintStream.print(Object) uses String.valueOf(Object), which can handle null values.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!