This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Type of null Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Type of null" Watch "Type of null" New topic

Type of null

Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
Hi everybody,
I just wanted to know the type of 'null'. The reason is that in a mock exam question a method is overloaded with one taking an Object as a parameter and the other one is taking a String as a parameter, and calling the method by passing 'null' as the parameter. The method which has the String parameter aperently gets called. So want to justify how this works. In the same time, if there was a overloaded version which accpets a StringBuffer as a parameter, the compiler generates a compile time error, saying the call to the method with a null parameter is ambiguous.
Thank you very much..

SCJP 1.4, SCMAD 1.0<br />SCWCD, SCBCD (in progress)
Yosi Hendarsjah
Ranch Hand

Joined: Oct 02, 2003
Posts: 166
Check this code:

Calling m1(null) will print "Stack" because class Stack extends Vector thus Stack is more specific than Vector. So the second method is called.
In the case of String and StringBuffer, neither of them is more specific than the other, so the compiler will complain.
[ February 15, 2004: Message edited by: Yosi Hendarsjah ]
Vineela Devi
Ranch Hand

Joined: Dec 20, 2003
Posts: 191
Hi desilva,
When a method is overloaded, and if the overloaded method is invoked by passing a parameter which is compatible with the parameters of both the methods, then the method which has more specific parameter is invoked.
Let me make this clear with an eg:

void method(int i){
System.out.println("Integer Method");
void method(float i){
System.out.println("Float Method");


suppose, i call method(1);
then the method which takes integer parameter will be invoked coz in this case it is more specific version.
In the same way,

void method(Object o){
System.out.println("Object Method");
void method(String s ){
System.out.println("String Method");

in this case where String extends Object and when u call the method passing null as argument to the overloaded methods, then String method will be invoked coz it is the more specific one here.

void method(StringBuffer sb){
System.out.println("StringBuffer Method Invoked");
void method(String s){
System.out.println("string Method invoked");

suppose if u call method(null), compiler will complain coz here there is an ambiguity abt the methods ie the compiler doesnot know which method to be invoked coz none is specific compared to the other
Same is the case when u have the following methods

void method(int i, float f){
System.out.println("First Method");
void method(float f, int i){
System.out.println("Second Method");

suppose if u pass parameters as method(1,1) ambiguity results.
Hope u got it.
[ February 15, 2004: Message edited by: Vineela Devi Jakka ]
Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
Ransika deSilva
Ranch Hand

Joined: Feb 18, 2003
Posts: 524
Thank you very much for all the answers. Got the concept very clearly.
jQuery in Action, 3rd edition
subject: Type of null
It's not a secret anymore!