This post demonstrates difference between
Look at the java bytecode of the program.
In code column, line #8 shows the bytecode representation of calling
Consider the bytecode, in code column line #18 shows the bytecode representation of calling
The documentation tells that the null reference is handled if null reference is passed. If argument is
Please share your thoughts on this post in the comments section.
print(null.tostring())
and print(null)
in Java programming.
The difference isConsider the below sample code to understand the concept clearly.print(null.tostring())
throws null pointer exception butprint(null)
doesn't. Why?
public class NPETest { public static void main(String[] args) { Object obj1 = null; Object obj2 = null; System.out.print(obj1.toString()); //throws Null Pointer Exception System.out.print(obj2); // prints null } }
The statement System.out.print(obj1.toString())
causes following runtime exception.
Exception in thread "main" java.lang.NullPointerException at com.org.s20150310072124635.NPETest.main(NPETest.java:5)
The statement System.out.println(obj2)
runs fine and prints a text "null".
Why an exception is caused?
Look at the java bytecode of the program.
> javap -classpath target\test-classes -c NPETest Compiled from "NPETest.java" public class NPETest extends java.lang.Object{ public NPETest(); Code: 0: aload_0 1: invokespecial #8; //Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: aconst_null 1: astore_1 2: aconst_null 3: astore_2 4: getstatic #17; //Field java/lang/System.out:Ljava/io/PrintStream; 7: aload_1 8: invokevirtual #23; //Method java/lang/Object.toString:()Ljava/lang/String; 11: invokevirtual #27; //Method java/io/PrintStream.print:(Ljava/lang/String;)V 14: getstatic #17; //Field java/lang/System.out:Ljava/io/PrintStream; 17: aload_2 18: invokevirtual #33; //Method java/io/PrintStream.print:(Ljava/lang/Object;)V 21: return }
In code column, line #8 shows the bytecode representation of calling
obj1.toString()
. Here obj1
is null
and so any attempt on a method invocation on null
results in a NullPointerException
.
Why print(null) works fine?
Consider the bytecode, in code column line #18 shows the bytecode representation of calling
print(obj2)
. Here object is passed as a parameter to the PrintStream.print()
method. According to the Java documentation, the source code will tell you that why this does not result in NullPointerException
.public void print(Object obj) { write(String.valueOf(obj)); } public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); }
The documentation tells that the null reference is handled if null reference is passed. If argument is
null
, then a string equal to "null"; otherwise, the value of obj.toString()
is returned. So this statement does not result in NullPointerException
.
Summary -
print(null.tostring())
causes Null Pointer Exception.
print(null)
prints text "null".
Please share your thoughts on this post in the comments section.