| 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
|
|
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]
|
 |
 |
|
|
subject: Difference between Null String and Null Object
|
|
|