Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing null to Obj,String args methods

 
Yogesh Chhawasaria
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Answers
The code does not compile.
The code compiles cleanly and shows "Object Version".
The code compiles cleanly and shows "String Version" <<CORRECT ANS>>
The code throws an Exception at Runtime.
Can anyone Plz explain How Compiler deduced that. Plz also mention the thumbrule for the same
 
Serghei Jelauc
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Both methods method(Object o) and method(String s) can take a null as parameter, but compiler decides whih of them most optimal. method(Object o) is most general but method(String s) most concrete and compiler chooses second method with String parameter.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Serghei is quite correct here. But the word "specific" is a better choice than "optimal" or "concrete". That is, the compiler chose String because it is more specific than Object (from which String is derived).
 
Serghei Jelauc
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Barry.
Thanks for correction. Unfortunately English is not my mother tongue and not my second language.
 
Yogesh Chhawasaria
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Answers
The code does not compile.
The code compiles cleanly and shows "StringBuffer Version".
The code compiles cleanly and shows "String Version"
What would be result now since now both strinbuffer and sting are both specific.
 
Serghei Jelauc
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compile-time error because reference to method is ambiguous.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic