This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what is "null"" Watch "what is "null"" New topic
Author

what is "null"

Angell Bulin
Greenhorn

Joined: Feb 26, 2002
Posts: 3
why the program1 have compile error, but program2 can compiled?
i think "null" is a object, is wrong?

//------program 1
public class Tester {
void test(Object s) { System.out.println("Object"); }
void test(SubTester s) { System.out.println ("SubTester"); }
void test(String s) { System.out.println ("String version"); }
public static void main (String args[]) {
Tester c = new Tester ();
}
}
class SubTester extends Tester{ }

//------program 2
public class Tester {
void test(Object s) { System.out.println("Object"); }
void test(Tester s) { System.out.println ("Tester"); }
void test(SubTester s) { System.out.println ("SubTester"); }
public static void main (String args[]) {
Tester c = new Tester ();
}
}
class SubTester extends Tester{ }
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
I see nothing wrong with both of your code... I tried them and they compiled and ran fine... without error...
Besides, null is not an object, null just means that a reference variable does not reference any object...


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
chafule razgul
Ranch Hand

Joined: Feb 09, 2002
Posts: 63
interestingly System.out.println(null); is not as innocent as it seems
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
I have no compile error with either program, but also no output. Here is a version with output.

[ February 27, 2002: Message edited by: Marilyn deQueiroz ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Angell Bulin
Greenhorn

Joined: Feb 26, 2002
Posts: 3
Valentin

if you compile and run these code, could you tell me the result :roll:
Angell Bulin
Greenhorn

Joined: Feb 26, 2002
Posts: 3
Marilyn
tester.test( new Object() );
tester.test( tester );
tester.test( new SubTester() );
tester.test( "" );
all these four call pass the explicit object as a parameter, but if you pass null to test(), which method will be used ?
Marcus Howarth
Ranch Hand

Joined: Jan 04, 2002
Posts: 37
Very strange, I'd like an explanation too if poss.
adding the following call
tester.test( null );
to the above code gives a compiler error stating "Reference to test is ambigious .. " it's a String and a SubTester..
when I added the above line to the original code provided it happily compiled and when passing a null (as above) would execute the overloaded test(SubTester st) method at runtime.
But the addition of a method that takes a String has changed this. When a tester.test( null ); call is made now it doesn't compile.

what's happening here? is there some strange casting rule applied to null that implements the String and sub-class overloaded methods??
[ February 27, 2002: Message edited by: Marcus Howarth ]

Marcus<br /> <br />SCJP, March '02
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
Hope that helps
Regards
Ravish
Marcus Howarth
Ranch Hand

Joined: Jan 04, 2002
Posts: 37
spot on!
Ambiguity comes from the fact that the compiler is looking for the most derived class. In this case it sees String and SubTester at the same level.
Also:
The null reference can always be cast to any reference type. In practice, the programmer can ignore the null type and just pretend that null is merely a special literal that can be of any reference type.

thanks Ravish!
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
I am not sure if this will help:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is "null"
 
Similar Threads
Method call
How does Java compiler resolve the ambiguity to decide which methods to call?
most specific method
Calling correct methods
Why an compile error for this code?