File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes null as a parameter to method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "null as a parameter to method" Watch "null as a parameter to method" New topic
Author

null as a parameter to method

Sandip Shinde
Greenhorn

Joined: Apr 28, 2005
Posts: 6
I have following code

public class Tester
{

//main method
public static void main(String[] args)
{
painter(null);///look here
}

public static void painter(Object obj)
{
System.out.println("Object Method");
}

public static void painter(String str)
{
System.out.println("String method");
}

}


now I see "String method" as output for this program , on which basis String parameter method is called while I am passing null as parameter?
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Here you have method painter() which you have overloaded in the same class Tester. Generally the term overloaded is used in case of inheritance, that is when we have a base class and a sub class, and than in the sub class we overlaod the method from the base class. Anyways,

Generally in the case of an overloaded method, the more specific of the two methods is selected. In your case the parameter which you are passing to your methods is "null".

Now, String is a subclass of Object, therefore "null" is more applicable to String rather than Object class.

Lets take another example to clarify it, lets replace "null" by a literal "8". Refer to the code below:



The output of this code will be Integer selected, although both the painter methods are applicable, the integer method is selected becuase it is more specific of the two.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

Note that the "most specific" method is chosen. That (essentially) means that the class that is most removed from the Object class. What happens if you have two classes that are equally specific?


You get this error:


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Generally the term overloaded is used in case of inheritance, that is when we have a base class and a sub class, and than in the sub class we overlaod the method from the base class. Anyways,

Replace the words "overloaded" with "overridden" and tell us if what you originally said is correct.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1824

The output of this code will be Integer selected, although both the painter methods are applicable, the integer method is selected becuase it is more specific of the two

Actually, it is because you are passing an integer. Specificality doesn't apply to primitives, as they do not extend one another. There's no confusion to the compiler because a literal 8 is always and forever an integer. To get a literal 8 as a long, you owuld have to use 8L.
Abdulla Mamuwala
Ranch Hand

Joined: Jan 09, 2004
Posts: 225
Catch your point guys, thanks, sorry for using the wrong terminology Sandeep.
Sandip Shinde
Greenhorn

Joined: Apr 28, 2005
Posts: 6
Thanks for great reply from u all,
Got the exact basis for overloded methods invocation(more specific one will be called .. clear enough)

Thanks again
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: null as a parameter to method