4

In Java, I understand that whenever we print object reference, internally toString() will be called. By default, toString() will print in this format "classname@hashcode". If this is true, then the following snippet should raise Null Pointer exception. Why does it not happen?

int[][] a = new int[3][];
System.out.println(a);  --> Prints [a@xxxxx
System.out.println(a[0]); --> Prints null (It should have thrown Null pointer Exception?)

Could some one help me understand this?

Javask
  • 123
  • 1
  • 1
  • 5
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Chris K. Palma Chwialkowski Mar 08 '17 at 17:21
  • 1
    The OP's not trying to fix a NPE, but rather trying to figure out why there isn't one being thrown. – azurefrog Mar 08 '17 at 17:22
  • 3
    The first println is printing an object reference which uses the default formatter, while the second is printing a pointer. Outputting a pointer does not dereference the pointer, just prints it. And Java has the formatter overloaded to check for null and print "null". Kinda nice really. – ScottK Mar 08 '17 at 17:23

4 Answers4

4

This happens because println() doesn't invoke toString(). Instead, it invokes String.valueOf(x), which checks if x is null to prevent NullPointerException.

Check these documentation pages (or just look into the source code of PrintStream):

PrintStream.println(Object)

String.valueOf(Object)

esin88
  • 3,091
  • 30
  • 35
  • Thank you very much. I've checked the source code. public static String More ...valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); } – Javask Mar 08 '17 at 17:34
  • BTW this : `System.out.println(String.valueOf(null));` invokes an NPE. – boxed__l Mar 08 '17 at 17:43
  • 1
    @Abhijith that's because `String.valueOf(null)` goes to the `static String valueOf(char data[])` method. It's because compiler chooses to invoke *the most specific* method that fits. And the method that takes `Object` is, obviously, less specific than method that takes `char[]`. You can look here: http://stackoverflow.com/a/1572499/1553934 – esin88 Mar 08 '17 at 17:50
1

It because in a 2D Array defined as [x][y] the x index holds the reference of the y which is a array of y elements.

So when you printed System.out.println(a);it gave its toString representtion. And when you inquired as System.out.println(a[0]); it was having a null reference hence it printed null

mhasan
  • 3,703
  • 1
  • 18
  • 37
0

println checks for null and instead print null, without calling toString. It is something like: println(Object x){ out.append( (x!=null)?x.toString():"null" ); out.append("\n"); }

0

a is not null, you declare what a is in the first line int[][] a = new int[3][]; you never declare or set a[0] however so it is null. You can print this value and pass this value around without causing a null pointer exception. You get a null pointer exception when you try and call a method on a null value. So you would get a null pointer exception if you called a[0].toString() or any other method on that null value.

https://docs.oracle.com/javase/7/docs/api/java/lang/NullPointerException.html

SeanKelleyx
  • 1,155
  • 13
  • 15