Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why java doesn't support pointers? [duplicate]

Tags:

java

Possible Duplicate:
Why can't we use pointers in Java?

I want to know are there any pointer concepts in java?

I know that there is no explicit declaration of pointers but implcitly pointer concept is implemented.Why there is no pointer in java

like image 978
user1214216 Avatar asked Mar 07 '12 04:03

user1214216


People also ask

Why does Java not make use of pointers?

Java do not use pointers because using pointer the memory area can be directly accessed, which is a security issue. pointers need so memory spaces at the runtime. to reduce the usage of memory spaces java does not support pointers.

What is the replacement for pointers in Java?

Java uses the (safer) idea of references instead of pointers. The Java language does _not_ provide pointers. Instead, all objects are handled by references, not to be confused with pointers or C++ references.

Can I use pointers in Java?

No Pointer Manipulation in Java Although a reference internally uses a pointer but Java does not allow any manipulation to an underlying pointer using a reference variable. It makes java more secure and robust.

Why pointer is not used in Java stack overflow?

Because the designers of Java thought it was a tricky-to-use and error prone construct. Which concept is used instead of pointer in Java? References (which are quite similar to pointers if you disregard from pointer arithmetic). Keep in mind that all objects you create, you create on the heap (using the new keyword).


1 Answers

You have to distinguish between several uses of pointers:

  • Memory access via pointer arithmetic - this is fundamentally unsafe. Java has a robust security model and disallows pointer arithmetic for this reason. It would be impossible for the JVM to ensure that code containing pointer arithmetic is safe without expensive runtime checks. You don't need pointer arithmetic unless you are writing extremely low level code (in which case you should probably be using assembler or C/C++ instead)
  • Array access via pointer offsets - Java does this via indexed array access so you don't need pointers. A big advantage of Java's indexed array access is that it detects and disallows out of bounds array access, which can be a major source of bugs. This is generally worth paying the price of a tiny bit of runtime overhead.
  • References to objects - Java has this, it just doesn't call them pointers. Any normal object reference works as one of these. When you do String s="Hello"; you get what is effectively a pointer to a string object.
  • Passing argument by reference, i.e. passing a reference which allows you to change the value of a variable in the caller's scope - Java doesn't have this, but it's a pretty rare use case and can easily be done in other ways. This is in general equivalent to changing a field in an object scope that both the caller and callee can see.
  • Manual memory management - you can use pointers to manually control and allocate blocks of memory. This is useful for some applications (games, device drivers) but for general purpose OOP programming it is simply not worth the effort. Java instead provides very good automatic garbage collection which takes care of memory management for you. This is an extremely good thing: for many people who had previously been forced to deal with manual memory management in Pascal/C/C++ this was one of the biggest advantages of Java when it launched.

So overall Java doesn't have pointers (in the C/C++ sense) because it doesn't need them for general purpose OOP programming. Furthermore, adding pointers to Java would undermine security and robustness and make the language more complex.

like image 163
mikera Avatar answered Oct 20 '22 19:10

mikera