Tuesday, May 10, 2011

NullPointerException and Java

When I first started programming in Java, that was like few years ago, with the little background that I had of C the first thing I learnt was, Java does not have Pointers

Why doesn't it have Pointers?
Structures were removed and gone, and arrays and strings are objects, the need for pointers to these constructs goes away.

Why Structures were removed?
You don't need structures and unions when you have classes; you can achieve the same effect simply by declaring a class with the appropriate instance variables.

So now we have enough reasons as to why Java didn't have Pointers.

Having said that it makes me wonder why is there an exception called NullPointerException when there is no Pointer in Java. I am very sure the Java developers would have some reason to have named this the way it is today. May be they could have named it NullReferenceException. But obviously they didn't. The following are my guesses as to why it can still be called so
  • Its not that Java does not have Pointers
  • In fact every reference in Java is ideally a Pointer (except for primitive types)
  • Its just that Java doesn't support Pointer arithmetic

No comments:

Post a Comment