Posts

Showing posts with the label null pointer exception

Dose Java has pointer ?

N o, Java does not have pointers. This was an intentional decision by the creators of Java, because most people would agree that having pointers creates a lot of potential for bugs in the code – pointers can be quite confusing, especially to new programmers. Because arrays and strings are provided as class types in Java, there is no need for pointers to those constructs. By not allowing pointers, Java provides effectively provides another level of abstraction to the programmer.Java has references, but not pointers.All  values of non primitive type / reference type are always stored as pointers in the memory.  Here are some of the  differences between references in Java and pointers in C++: 1 .  References store an address.  That address is the address in memory of the object.  So, when a class is declared like so:  "PersonClass y = new PersonClass();" the "y" variable actually stores an  address in memory. If you w...

There is no concept of pointers in Java, but still we get null pointer exceptions. Why?

The premise of the question is wrong, because there  is  a concept of pointers in Java. They are called references, and the places where they are used differ from some other languages, but they are pointers nevertheless. In Java, there are only primitive types and reference types. So all non-primitive values are  references . A "reference" is defined ( JLS 4.3.1 ) as either a  pointer to an object , or the null reference (which is a null pointer, hence the "null pointer exception").