Question:
why null can be passed as a string parameter ? see the code.?
geosongeorge
2008-09-16 10:51:36 UTC
class TestClass{
public void callMethod(Object obj){
System.out.println("hi");
}
public void callMethod(String s){
System.out.println("By");
}
public static void main(String[] args) {
Hai i= new Hai();
i.callMethod(null);


}

}
if we pass null in callMethod, 'By' will be the output.why?
Five answers:
richarduie
2008-09-16 12:41:07 UTC
Either of the two method signatures would be satisfied to receive a null argument, since both Object and String can be null. When competition arises between choices of signatures for overloaded methods, the language specification resolves the choice to the most type-specific signature available. Since String is more specific than Object, the method with the String parameter is elected.



You can play with this more:



public void whichMethod( Object o ) {

// least specific - can handle any type

System.out.println("I handle Objects");

}



public void whichMethod( Number n ) {

// more specific can handle any Number type

System.out.println("I handle Numbers");

}



public void whichMethod( Integer i ) {

// most specific - handle Integer type

System.out.println("I handle Integers");

}



As some examples...



Argument: String - Signature: whichMethod( Object )

Argument: Double - Signature: whichMethod( Number )

Argument: null - Signature: whichMethod( Integer )
deonejuan
2008-09-16 11:48:47 UTC
Your code, as typed above, will NOT work.

============

/**

* Created on Sep 16, 2008, 8:18:05 PM

*/



public class TestNull {



public void callMethod(Object obj) {

System.out.println("hi");

}



public void callMethod(String s) {

System.out.println("By");

}



public static void main(String[] args) {

TestNull i = new TestNull();

i.callMethod(null);



Object obj = new Object();

i.callMethod(obj);

}

}

//////////////////



$> run-single:

By

hi

===============

Because I made an Object of type TestNull in main, I get state, values, and methods. The JRE will put an object on the heap something like this:



[$a$1h[TestNull::i[callMethod[String]:null::callMethod[Object]:null]]



consider:

static int i; // will equal 0

static double d; or static float f; // will equal 0.0f

static Object obj; // will equal null

static String s; // will equal null

static boolean b; // will equal false



the keyword static will init the value of a var FIRST when making any Object. If there is no assignment operator (=) then the JRE will use the defaults mentioned above
Ahmed The Ninja
2008-09-16 10:56:45 UTC
Ah, reminds me of days when I was preparing for SCJP. I believe the output is "By", because Java selects a more specific data type.



Regarding first question, why can null be passed as a String? Because null is an appropriate value for a String (or any other object).
sonofqc
2008-09-16 11:00:26 UTC
Looks like Java.



Not sure what Hai is. The null is a string not an object, that is why

you are getting "By" as a return.
once4ever
2008-09-16 11:45:48 UTC
As you see null is string so debuger fire method with string input but if you use null object instead of null you will see new result.

object nulobj = null

i.callMethod(nulobj);



good luck


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...