Does java really have pointers or not?

I have looked on google for answers but I am not satisfied.

My Logic:

Java uses memory locations, it's just behind the scenes where you can't see or access it (to my knowledge, probably there are ways of accessing them that I don't know).

My Confusion / Question :

What is the purpose of not having pointers in a programming language like Java, designed specifically for the internet to be used on any system, vs a programming language like c++, which does use pointers?

Edit


Many of you are saying "To keep it simple". If this is the case, then why does a popular programming language, like c++ , use pointers anyway?


The simple answer is that it is a design decision. The slightly longer answer is that pointers in C++ are only necessary for memory manipulation, which is not a concept that applies to Java (see below).

Java envisions a fairly systematic, object-oriented programming model, and all class-based types are essentially always handled through a pointer, and so this fact isn't exposed to the user at all.

Raw pointers in C++ aren't very necessary in high-quality, systematic and idiomatic programming, either. What raw pointers do allow you to do (namely pointer arithmetic) is generally considered "unsafe", and so it is simply left out of Java altogether.

Note by the way that many things people attempt to do with pointers in C and C++ is actually undefined behaviour. Using pointers correctly leaves you with a fairly restricted set of options, most of which can be done better in idiomatic C++.

About the only real use for pointers is direct memory manipulation. Since Java doesn't want you to do that (and in fact its garbage-collected memory management would actively interfere with and be broken by manual memory manipulation), there's no need for explicit pointers.

After your update: The last paragraph is (in my opinion) the most striking explanation: In C++ you need to be able to write your own memory managing code (cf. "allocators"). And memory has to be handled via pointers. So my strict answer is that you need pointers in C++ when you're implementing the memory management, and never otherwise.


Internally a reference to an object is implemented as a pointer. There is no pointer arithmetic though...

C++ has pointers because it is built as a superset of C, which does have pointers. C has pointers because it was designed in the 60's. At that time computers had very little memory and pointers allowed the implementation of Strings, arrays and parameter passing.

This is an extract from the White Paper The Java Language Environment:

2.2.9 No More Pointers

Most studies agree that pointers are one of the primary features that enable programmers to inject bugs into their code. Given that structures are gone, and arrays and strings are objects, the need for pointers to these constructs goes away. Thus, Java has no pointer data types. Any task that would require arrays, structures, and pointers in C can be more easily and reliably performed by declaring objects and arrays of objects. Instead of complex pointer manipulation on array pointers, you access arrays by their arithmetic indices. The Java run-time system checks all array indexing to ensure indices are within the bounds of the array.

You no longer have dangling pointers and trashing of memory because of incorrect pointers, because there are no pointers in Java.


Java does have pointers. In fact, everything in Java is pointers (called references).

Exceptions: int, boolean, char, etc.

As everything is pointers, no need for an asterisk to make a difference.

链接地址: http://www.djcxy.com/p/31656.html

上一篇: 为什么我们不能在Java中使用指针?

下一篇: java真的有指针吗?