no pointer in java

Why there are no pointers in Java?

  • In Java there are references instead of pointers. These references point to objects in memory. But there is no direct access to these memory locations. JVM is free to move the objects within VM memory.
  • The absence of pointers helps Java in managing memory and garbage collection effectively. Also it provides developers with convenience of not getting worried about memory allocation and deallocation.

 

If there are no pointers in Java, then why do we get NullPointerException?

  • In Java, the pointer equivalent is Object reference. When we use a . it points to object reference. So JVM uses pointers but programmers only see object references.
  • In case an object reference points to null object, and we try to access a method or member variable on it, then we get NullPointerException.
posted @ 2019-04-03 11:02  kissrule  阅读(231)  评论(0编辑  收藏  举报