Why doesn't System.out.println () throw a NullPointerException?

This may be a very simple question, but I still don't know the answer.

String abc = null; System.out.println(abc); 

Why does System.out.println print "null" and not throw a NullPointerException ?

+6
source share
7 answers

Since it ultimately reaches the print method, which prints "null" for a null String :

 public void println(String x) { synchronized (lock) { print(x); println(); } } public void print(String s) { if (s == null) { s = "null"; } write(s); } 

The same behavior exists for printing any null reference (in this case, the string "null" returns String.valueOf (null):

 public void println(Object x) { String s = String.valueOf(x); synchronized (lock) { print(s); println(); } } public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); } 
+5
source

It behaves as it is documented. PrintStream.println(String) documented as:

Print a line and then end the line. This method behaves as if it calls print(String) and then println() .

PrintStream.print(String) documented as:

Prints a string. If the argument is null , then the string "null" is printed. Otherwise, string characters are converted to bytes according to the default character encoding of the platform, and these bytes are written exactly in the write(int) .

If in doubt, read the documentation :)

+10
source

You can just look at the source code of PrintStream :

 public void println(String x) { synchronized (this) { print(x); newLine(); } } public void print(String s) { if (s == null) { s = "null"; } write(s); } 

As you can see, the null case is handled by printing "null".

+2
source

Null has a special wrapper inside the print function:

 public void print(String s) { if (s == null) { s = "null"; } write(s); } 
+1
source

According to PrintStream # println ()

Print the object and then end the line. This method calls String.valueOf (x) first to get the string value of the printable, and then behaves as if it calls Print (String) and then println ().

According to String # valueOf

Returns a string representation of the Object argument. If the argument is null, then the string is null; otherwise, obj.toString () is returned.

0
source

The source code of the System.out.print function. If the print string is null, it sets to null

 public void print(String s) { if (s == null) { s = "null"; } write(s); } 
0
source

Well, in some cases, System.out.println may throw a NullPointerException making you think that this works.

If you have a complex object in which you created your own toString() method, there is a chance of an error in this method. You can accidentally or intentionally encode this so that NPE does occur. The object itself is not null, however some of the attributes inside may be empty.

Such coding is likely to be discouraged, although it violates the contract, as others have pointed out. The desired behavior should represent the null object as null , and not preempt the NPE.

0
source

Source: https://habr.com/ru/post/981254/


All Articles