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

  • Method Overloading:
        1.public void prinltln(String str) { }
        2.public void prinltln(char[] ch){ }
        3.public void prinltln(Object ch){ 
  •  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 .
  • Compilation Error:
       System.out.println(null)

Program #1: what will happen when we print System.out.println(null)


System out prinln null

  • Compile Fine:
         System.out.println((String)null);//null
         System.out.println((char[])null);
         System.out.println((Object)null);//null
  • It's the compiler type-checking the parameters of the method call.
  • But here we need to know one more thing  System.out.println((char[])null); will compile fine but at run time will throw runtime exception.

 Program #2: what will happen when we print System.out.println(null) by type casting
  

  1. package com.systemoutprintn;
  2. public class Test {
  3.  
  4.     /**
  5.      * @Website: www.instanceofjava.com
  6.      * @category: System.out.println(null)
  7.      */
  8.  
  9. public static void main(String[] args) {
  10.  
  11.          System.out.println((String)null);//null
  12.          System.out.println((Object)null);//null
  13.          System.out.println((char[])null);
  14.             
  15.  
  16. }
  17.  
  18. }

 Output:
 

  1. null
  2. null
  3. Exception in thread "main" java.lang.NullPointerException
  4.     at java.io.Writer.write(Unknown Source)
  5.     at java.io.PrintStream.write(Unknown Source)
  6.     at java.io.PrintStream.print(Unknown Source)
  7.     at java.io.PrintStream.println(Unknown Source)
  8.     at com.systemoutprintn.Test.main(Test.java:13)


Instance Of Java

We will help you in learning.Please leave your comments and suggestions in comment section. if you any doubts please use search box provided right side. Search there for answers thank you.
«
Next
Newer Post
»
Previous
Older Post

4 comments for What happens When System.out.println(null)?

  1. System.out.println((char[])null);

    This statement will not work. Because according to the javadocs, the char type will not handle null pointer which leads to a null pointer exception. String and Object works fine as you said.

    -Regards,
    Karthik Byggari

    ReplyDelete
  2. You are telling the compiler which implementation of System.out.println(*) to use, but when trying to use it execution fails... Interesting... Testing should lead you to validate that output...

    ReplyDelete
  3. class J
    {

    public static void main(String [] args)
    {

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

    }

    }


    this code is compile fine but runtime error like

    Exception in thread "main" java.lang.NullPointerException
    at java.io.Writer.write(Writer.java:127)
    at java.io.PrintStream.write(PrintStream.java:503)
    at java.io.PrintStream.print(PrintStream.java:653)
    at java.io.PrintStream.println(PrintStream.java:792)
    at logicalquestion.J.main(J.java:9)

    ReplyDelete
    Replies
    1. This is correct and exactly what is mentioned in the last line of the post:
      "But here we need to know one more thing System.out.println((char[])null); will compile fine but at run time will throw runtime exception."

      Delete

Select Menu