Why Java does not support Pointers?


Java designers keep only two things in their mind while language is being developed – a) to make programming simple to practice and 2) to increase performance. Towards this goal only Java does not support Pointers.
Reasons for Java does not support Pointers:

1. The first and very important reason is the security. The concept of Pointers in C-lang is often misused and is probably the biggest reason for people considering C-lang a complicated programming language.

The creators of Java did not like to introduce this complexity into their programming language and intentionally excluded the concept of 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 given the status of classes with methods of manipulating the elements, there is no need for pointers to those constructs.

2. By not allowing pointers, Java effectively provides another level of abstraction to the programmer. No pointer support make Java more secure because they point to memory location or used for memory management that loses the security as we use them directly.

3. To make program simple, they have eliminated pointers. Java proved a language can exist without the support of pointers. Pointers is a big menace in C/C++ and many books are written exclusively on pointers. Non-support of pointers eliminate (unnecessary) changing the value from a different location. The code becomes clean and understandable reducing complexity.

For example, pointers allow the other function values to change which the function may not like. For example, some function would like the $ value as Rs.61.5. The other function may change the value of $ with pointer support. Then the original function looses its value integrity.

4. The simplest explanation is that it would be almost impossible to create a working of a Garbage Collector (GC), if pointers are available. With only references, it is possible to construct efficient garbage collector algorithms that can eliminate memory fragmentation, especially in case of long running processes.

In case of pointers, if a GC moves an object in the memory, it would need a way locate and update all pointers pointing to the moved object. The garbage collection process becomes very slow and error prone.

With the introduction of the JVM, the use of GC has eliminated the need to explicitly de-allocate memory like in C/C++. The explicit means of de-allocating memory from an object in Java is handled implicitly by the GC daemon thread, which means that an object that reaches out of scope in memory is automatically sent to the GC for garbage collection. This feature of Java coupled with the original intent of the creators made it a necessity to eliminate complexity and thus introduced a more managed approach to dynamic memory.

Which concept is used instead of pointer in Java?

References (which are quite similar to pointers if you disregard from pointer arithmetic).

In Java we have references which are close to pointers but not same. References store the address of the object in memory. Pointers can be manipulated (like arithmetic operations of addition etc.) but references cannot be manipulated.

Pass your comments on this tutorial " Java does not support Pointers ".

Leave a Comment

Your email address will not be published.