This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Null Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Null Problem" Watch "Null Problem" New topic
Author

Null Problem

Ramesh Sahu
Ranch Hand

Joined: Jul 14, 2008
Posts: 32
code:
--------------------------------------------------------------------------------


class AQ{
public void math(Object o){
System.out.println("ob ver");
}
public void math(String o){
System.out.println("str ver");
}
public static void main(String str[]){
AQ q=new AQ();
q.math(null);
}
}


--------------------------------------------------------------------------------

when I run the above code the output is
str ver

why?
[ July 31, 2008: Message edited by: Ramesh Sahu ]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

-----------------------------------------------------------------
Java choose nearly(closest) matching method for the reference
-----------------------------------------------------------------
Ramesh Sahu
Ranch Hand

Joined: Jul 14, 2008
Posts: 32
how can a null be matched to anything else?
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
when I run the above code the output is
str ver


Hi
I guess you are aware with the widening concept. In which if there are methods
void m (int i)
void m (Integer i)
void m (Object o)
void m(Object... args)

if an int i =5 is passed

so the preference wiil be given in the order
primitive type
wrapper class
object
varArgs

So considering in your case, method with String parameter will be called


<a href="http://technologiquepanorama.wordpress.com" target="_blank" rel="nofollow">My Techie Blog</a><br /><a href="http://www.java-questions.com" target="_blank" rel="nofollow">Java Questions</a>
Harshit Rastogi
Ranch Hand

Joined: Apr 15, 2008
Posts: 131
how can a null be matched to anything else?


you can define

String str = null;

I guess this will answer the question.
Sandeep Bhandari
Ranch Hand

Joined: Apr 16, 2004
Posts: 201
Just to add, its working the same way with 1.4 and 1.3 too.



makes the call to math method ambiguous

and the following code



results in the output of

"ob ver"

Hope that makes some sense
[ July 31, 2008: Message edited by: Sandeep Bhandari ]

SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
Sandeep Bhandari
Ranch Hand

Joined: Apr 16, 2004
Posts: 201
also guess the output of following program


Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Originally posted by Sandeep Bhandari:
System.out.println("ob ver");


Hai, you are giving same message in both a case...anyway here object reference will take precedence
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Null Problem