Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does java really have pointers or not? [closed]

Tags:

java

c++

pointers

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?

like image 484
Gabriel Avatar asked Nov 10 '11 13:11

Gabriel


People also ask

Why does Java have no 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.

How does Java get rid of pointers?

No, it is not possible to remove all "pointers" to a given object, for one because Java doesn't have pointers, they are called references. Neither Java nor the JVM knows all references to a given object. This is why the garbage collector has to scan everything to find unused objects.


1 Answers

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.

like image 152
Kerrek SB Avatar answered Oct 09 '22 03:10

Kerrek SB