2

I want to know why the below code doesn't work :

System.out.print(null);    
response.getWriter().print(null);

But the below ones work :

String s = null;
System.out.print(s);
response.getWriter().print(s);

Whats the difference between passing a null as compared to passing a reference as null ?

EDITED : Doesn't work fore mentioned indicates to compilation error .

6
  • 4
    Define "doesn't work."
    – Matt Ball
    Feb 9, 2013 at 3:41
  • Interesting. I suppose that just passing a null makes it so that the program treats it as nothing while passing a null through a reference works since it's basically showing that the variable itself has no value.
    – David
    Feb 9, 2013 at 3:43
  • the initial null isnt bound to an object. The second is a string which contains null data. Likely this would return the same as "" which would be accepted by the compiler fine
    – RyanS
    Feb 9, 2013 at 3:43
  • 1
    "Doesn't work" == "Doesn't compile" or "Doesn't work" == "Does not run"? Feb 9, 2013 at 3:43
  • @RyanS an empty string is decidedly different from a null string.
    – Matt Ball
    Feb 9, 2013 at 3:43

3 Answers 3

9

This is because you can pass an Object or a String. Since null can fit in both, the compiler doesn't know which method to use, leading to compile error.

Methods definitions:

Instead, if you provide an Object or a String variable (even if it has null value), the compiler would know which method to use.

EDIT: This is better explained in this answer. As to the internal link pointing to the Java specification, you can read it here, and this case would suit here:

The informal intuition is that one method is more specific than another **if any invocation handled by the first method could be passed on to the other one without a compile-time type error.

It is possible that no method is the most specific, because there are two or more methods that are maximally specific.

8
  • 2
    This is called "overloading"
    – Ryan Amos
    Feb 9, 2013 at 3:46
  • I don't think that it's that it could be either, but it's not either. System.out.println() expects something non-null.
    – Stephen
    Feb 9, 2013 at 3:51
  • @Raufio if you test the 2nd code block provided by OP (without the response... part), in a simple public static void main(String[] args) method, the program will print null and terminate with no problems. Feb 9, 2013 at 3:54
  • @Luiggi: Is it really that ambiguous for the compiler ? Since null is a defined keyword in Java , designers of the compiler know that if its null they have to print null , doesn't matter whether its a String reference or Object reference ! Null is Class agnostic !!
    – AllTooSir
    Feb 9, 2013 at 3:55
  • 1
    @LuiggiMendoza It's for a deeper understanding about how the error occurs, not just the surface level of it.
    – Ryan Amos
    Feb 9, 2013 at 4:08
3

It's because System.out.println() expects something with a type. null doesn't have a type, and therefore it can't be output on it's own. This is shown by:

Doesn't work:

 System.out.println(null);

Works:

 System.out.println((String)null);
 System.out.println((char[])null);
 System.out.println((Object)null);

It's the compiler type-checking the parameters of the method call.

2

Thank you all for your answers . From your inputs , I compiled the answer myself . It seems the call System.out.print(null) is ambiguous to compiler because print(null) here will find the two best specific matches i.e. print(String) and print(char[]) . So compiler is unable to determine which method to call here .
Small example will be :

private void methodCall(String str) {
}

private void methodCall(char[] ch){
}

Now this code becomes ambigious : methodCall(null) .

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.