• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

what is "null"

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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{ }
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
interestingly System.out.println(null); is not as innocent as it seems
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 38
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 5397
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure if this will help:
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic