File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Type of null

 
Ransika deSilva
Ranch Hand
Posts: 524
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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..
 
Yosi Hendarsjah
Ranch Hand
Posts: 166
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
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
Posts: 191
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
similarly,

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.
Vineela.
[ February 15, 2004: Message edited by: Vineela Devi Jakka ]
 
Ransika deSilva
Ranch Hand
Posts: 524
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!,
 
Ransika deSilva
Ranch Hand
Posts: 524
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!,
Thank you very much for all the answers. Got the concept very clearly.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic