Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is "null"

 
Angell Bulin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
chafule razgul
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
interestingly System.out.println(null); is not as innocent as it seems
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Angell Bulin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Valentin

if you compile and run these code, could you tell me the result :roll:
 
Angell Bulin
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 38
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
R K Singh
Ranch Hand
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hope that helps
Regards
Ravish
 
Marcus Howarth
Ranch Hand
Posts: 38
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure if this will help:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic