my dog learned polymorphism
The moose likes Beginning Java and the fly likes Difference between Null String and Null Object Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Difference between Null String and Null Object" Watch "Difference between Null String and Null Object" New topic
Author

Difference between Null String and Null Object

Roshan Cherian
Greenhorn

Joined: Nov 26, 2007
Posts: 1
Please refer to the code snippet

class Test{
public static void getString(Object obj){
System.out.println( "object " + obj );
}

public static void getString(String obj){
System.out.println( "String " + obj );
}
public static void main(String args[])
{
new Test().getString(null);
}
}

While I ran this I am getting the output String null.Why is it so??Why it takes the 2nd method getString(String obj)??
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
First, I would re-write it to stop my compiler from complaining about static access to an instance:


You want to read up on overload resolution.

When you finally get down to the third rule (which this question does), the compiler picks the most specific invocation it possibly can. Since String is more specific than Object, and null can be either, String is chosen.
[ November 26, 2007: Message edited by: Bill Shirley ]

Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24081
    
  15

Hi,

Welcome to JavaRanch!

First, a bit of business: you may not have read our naming policy on the way in. It requires that you use a full, real (sounding) first and last name for your display name. A single name isn't enough. You can change your display name here. Thanks!

Also, we have many forums here at JavaRanch; this one is for discussing the Enterprise Java Beans APIs, which doesn't seem to pertain to your question. I'll move this to our Beginner's Java forum, where it is on-topic.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between Null String and Null Object
 
Similar Threads
Object vs String
Most specific method.......
instanceof Doubt again??
Need clarification on overloaded method?
Why? null is not a common object,then what it is?