Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Passing null to Obj,String args methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Passing null to Obj,String args methods" Watch "Passing null to Obj,String args methods" New topic
Author

Passing null to Obj,String args methods

Yogesh Chhawasaria
Ranch Hand

Joined: Apr 02, 2004
Posts: 53

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


When you have eliminated all which is impossible, then whatever remains, however improbable, must be the truth.
Serghei Jelauc
Ranch Hand

Joined: Jul 24, 2002
Posts: 128
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.


SCJP 1.4 <br />SCBCD 1.3<br />SCWCD 1.4
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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).


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Serghei Jelauc
Ranch Hand

Joined: Jul 24, 2002
Posts: 128
Hi Barry.
Thanks for correction. Unfortunately English is not my mother tongue and not my second language.
Yogesh Chhawasaria
Ranch Hand

Joined: Apr 02, 2004
Posts: 53

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

Joined: Jul 24, 2002
Posts: 128
Compile-time error because reference to method is ambiguous.
 
Don't get me started about those stupid light bulbs.
 
subject: Passing null to Obj,String args methods
 
Similar Threads
Mock test Q
Constructor Overloading
question in javaquiz
Regarding String
String and Object class