This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Use of null in method call 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 "Use of null in method call" Watch "Use of null in method call" New topic
Author

Use of null in method call

Sandeep Ghosh
Ranch Hand

Joined: Jan 23, 2002
Posts: 145
Hi
Could anybody give the explanation regarding this problem.
Read the code below. Will be the result of attempting to compile and run the code below.

Prob 1
public class AQuestion
{
public void method(Object o)
{
System.out.println("Object Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
Answers
The code does not compile.
The code compiles cleanly and shows "Object Version".
The code compiles cleanly and shows "String Version"
The code throws an Exception at Runtime.
Prob 2
Read the code below. Will be the result of attempting to compile and run the code below.

public class AQuestion
{
public void method(StringBuffer sb)
{
System.out.println("StringBuffer Verion");
}
public void method(String s)
{
System.out.println("String Version");
}
public static void main(String args[])
{
AQuestion question = new AQuestion();
question.method(null);
}
}
Answers
The code does not compile.
The code compiles cleanly and shows "StringBuffer Version".
The code compiles cleanly and shows "String Version"
The code throws an Exception at Runtime.
Output of first prob is String Version....and second prob compile time error Why???
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
When two methods have the same name but a different parameter type, the most specific one is invoked.
In the first problem, one takes an Object and the other one a String as argument. A String is more specific than an Object because String is a subclass of Object, and thus, the method taking a String as argument gets invoked.
In the second problem, one method takes a String and the other one a StringBuffer as argument. String and StringBuffer are in no way related to each other, we say that they are sibling classes in the class hierarchy because they both are subclasses of Object but none is a subclass of the other. Thus, when giving null as argument, the compiler has no way to know which method to invoke and an error is raised.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Use of null in method call
 
Similar Threads
about overloading
About null
Doubt in overloaded method related question
Pass a Null as parameter?!
Mock Exam Questions (null)