File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overloading question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overloading question" Watch "overloading question" New topic

overloading question

Paula Jiang

Joined: Feb 15, 2003
Posts: 15
What will happen when you compile and run the following code?

SCJP2 1.4 (96%) SCWCD(96%)
Mellihoney Michael
Ranch Hand

Joined: Nov 27, 2002
Posts: 124
compiler will generate an error because the call

in the main() method is ambiguous.It means the complier don't know what method to call when it bounds the parameter "null"!

or the

I just compiled it

a beginner in java
Sridhar Srikanthan
Ranch Hand

Joined: Jan 08, 2003
Posts: 366
compiler will generate an error because the call

Compiler will not generate any error. Infact the program also runs and gives the result of m2.
This is because the compiler will map null to the "most specific object" that null can be cast into which is String.
The rule is String is-a Object but Object is-not-a String .
so the null is cast into a string.
I remember reading it somewhere in JLS via one of Dan's mock exams. Search the post and you will find the solution in one of the posts
[ February 15, 2003: Message edited by: Sri Sri ]
Paula Jiang

Joined: Feb 15, 2003
Posts: 15
it compiles and runs fine just like Sri Sri said, but I want to know why. Thanks Sri sri
Valentin Crettaz
Gold Digger

Joined: Aug 26, 2001
Posts: 7610
Related discussions:
null arg in overloaded method
null argument to a method - what is being passed??

[Blog] [Blogroll] [My Reviews] My Linked In
I agree. Here's the link:
subject: overloading question
It's not a secret anymore!