Is it possible to use Java pointers?

Asked

Viewed 10,773 times

12

In the C language we can use ponteiros as follows:

int *ponteiro, teste;

ponteiro = &teste;

And in Java there is some alternative?

  • look @Rafael found something interesting https://flaviowbrasil.wordpress.com/2009/11/ponteiros-no-java/

3 answers

10


There is no possibility of using raw pointers in Java. Even references have only implicit use through instantiated objects.

It is possible to simulate them through a array of bytes but just to demonstrate how it would work, it would hardly have a practical use, and yet it doesn’t mean that the language has pointers, only that its semantics can be emulated with other constructions.

This is not to say that the code does not use internal pointers but as they are not exposed in any way to the programmer, they cannot be considered as existing in the language. So there are opaque pointers that you don’t have access to.

It’s rare that they’re useful - essentially for performance, but it’s really a shame not to have them in those cases. C# allows them in contexts unsafe and so it’s possible to create some secure abstractions and get the best of both worlds. In Java if you want to use the pointer just doing in another language and it’s nothing simple.

Pointers X references.

7

Everything in Java that is not primitive is an object, objects are referenced by reference variables, in a way very similar to C pointers++.

That is, what you know as a C++ pointer is known as a Java reference.

The differences do not stop in the name, the biggest difference in fact is that in Java you do not work directly with the memory address, allocation and cleaning of the allocated memory. In Java you have the new, but it doesn’t have the delete. This is not to say that the memory allocated when creating a new object will never be released, this work is done by the Garbage Collector, which from time to time clears the objects from memory that no longer have a valid reference to them.

Answering your question: It is not possible to have a pointer in the same way as in C++, but it is possible to have a reference variable that will probably meet your needs. Java has been architected differently than C++, it is higher level, so if you need to work with direct access to memory it is very likely that Java nay will be your choice.

An example of the creation and instantiation of an object in Java:

Object obj = new Object();

0

You can create a pointer on an object. For example:

Object A = new Object(); //New trata de reservar memória o suficiente para o objeto e criar automaticamente uma referência a ele. 
A.setValue("valor qualquer");
Object B = A;//Aqui o B aponta para o endereço de memória de A
System.out.printf(B.getValue.toString());
//Resultado impresso: valor qualquer

A.setValue("outro valor");
System.out.printf(B.getValue.toString());
//Resultado impresso: outro valor

B.setValue("valor posto por B");
System.out.printf(B.getValue.toString());
//Resultado impresso: valor posto por B

Browser other questions tagged

You are not signed in. Login or sign up in order to post.