Does Java have 'pointers

Recently I’ve been having a little debate with someone over whether Java has pointers or not. In one of the video comments I called ‘this’ keyword a pointer. The debate sparked from there.

From what I understand, a pointer is a loose term used in programming and it’s general purpose is to classify something used to refer to something else. In computer programming this ‘something else (pointee)’ is typically memory. Now, my argument is that Java references are indeed pointers under the hood (not explicit pointers per se). Since references work by an alias to a memory address, referring to an ordered list of memory within the JVM, it complies with all the aspects of what a pointer should do except for direct manipulation of the memory values (which is what Java tries to do on its own and prevent programmers from doing). JVM memory is allocated by the computer memory, hence essentially a program is still operating with computer memory. However, Java ‘references’ are not explicit like that from languages such as C or C++, since you cannot directly read from the memory. In other words, Java pointers have been covered up by the language design.

The opposition insists that Java does not have pointers. Stating that a pointer reads directly from computer memory and not from the Java VM, which fails to comply with what a pointer does. He acknowledges that Java does not have C+±like pointers, but does not accept that Java can have their own so-called ‘pointers’. According to him, a pointer must be able to carry out arithmetic operations and read DIRECTLY from the computer memory and not from JVM memory. He asserts that references and pointers are totally different concepts and reading memory from a JVM is not the same as reading from computer memory (to quote him “these two are totally different concepts and must not be mixed up”). Therefore calling Java references and ‘pointers’ confuses people with the supposed ‘true definition’ of pointer like that in C/C++, and it should stop before more ‘debates’ over this spark again.

As it turns out with most online ‘debates’, the thing got quite heated with him getting very frustrated and repeating his points. I’d like to hear the community’s opinion over this seemingly pointless (see what I did there :D) debate.

[EDIT] I know that I have muddled up a few things in the original post because I wasn’t thinking straight. What I’m trying to say is that Java References embed the concepts of a ‘pointer’ within it, hence Java does have pointers. While programmers are not really able to access this pointer to do arithmetic operations, it still exists and works in the background.

They’re not pointers. With pointers you can do all sorts of things like pointer arithmetic, point them to anything you like, etc. With references, you cannot.

Cas :slight_smile:

I’m terribly sorry, the title is so wrong. I was meant to have the title as “Does Java have pointers” rather than “Are Java References, Pointers”. You see, after a bit of argument and so much coding, you’re out of brain juice for this kind of stuff. But thanks for the refreshment of concept though :slight_smile:

u can use http://www.docjar.com/docs/api/sun/misc/Unsafe.html … which is not pretty but you can use it to read and write directly. classes/instances are things in memory too, so you can access/modify those if you like.

it’s not as “natural” as with c/c++/opencl, but does the same.

Oh, okay that’s interesting. Yeah it definitely is not as safe as C/C++ when it comes to handling memory explicitly.

method handles references are typed function pointers. class object references are typed data pointers. array references are typed and based primitive pointers. arrays are the only ones you can (without stepping outside of the box) perform limited arithmetic with.

EDIT: I typed handle instead of reference. A method handle is something else…well, a handle.

Im not sure if you would consider the dot operator a pointer because unlike Object foo = bar , foo.bar(); which only modifies foo not bar , with the . operator if you were to do bar.bar()? I dont know.

I come from the school of thought: think low-level, write high level. Dot can be a pointer deference or not…static/optimistic analysis will determine.

Hmm I think this question stems from a word ambiguity. After all pointers vary across languages and they don’t do the same things. This quote I found really sums it up well.

[quote]"Like most disagreements, the core issue comes down to the definitions – what exactly does the term “pointer” mean? Conceptually, a “pointer” is just something that “points” to something else. Does it need to have any other features?

People who come from other languages which use the term “pointer” may have pre-conceived ideas of what constitutes a pointer, based on how it is used in that language. But that does not mean that a “pointer” has to be like what it is in that language. C/C++ pointers support pointer arithmetic; is pointer arithmetic an essential feature of a pointer? Go has pointers and does not support pointer arithmetic. Is the ability to “dereference” the pointer essential to the concept of a pointer? You need to come up with a precise definition of what you mean by “pointer” or otherwise this question is meaningless.

Java’s references are pretty much semantically identical to pointers to objects in C++ (other than the things that C/C++ pointers can do that don’t make sense in Java, like pointer arithmetic, dereferencing a pointer, and taking the address of a variable)."
[/quote]

thought you mean a pointer like an address/long.

I won’t be surprised if you call the ‘pp’ variable in the code below as ‘pointer to pointer’ :P:


public interface ISome {
}

public class SomeClass  implements ISome {
   
}

public class Delegate implements ISome {
   private ISome delegate;

   public void setDelegate( ISome delegate ){
       this.delegate = delegate;  
   }
   
   public ISome getDelegate() {
       return delegate;
   }

}
...

ISome p = new SomeClass();
ISome pp = new Delegate();

pp.setDelegate(p);
...


A wild stranger has appeared!

Java has NullPointerExceptions, so it must have pointers :slight_smile:

Shi have it but they hidden in JVM.

if you need something similar like pointers you can use (without harass with JVM and unsafe)

Object that hold variable object - i use it like pointer for primitive types

Yes its some crap code but technical its pointers)


public class Byte_Obj{
	public byte b;
	public Byte_Obj(){}
	public Byte_Obj(byte b){this.b = b;}
	public byte get(){return b;}
	public void set(byte b){this.b = b;}
	public void inc(){b++;}
	public void dec(){b--;}
	public String toString(){
		return new String ("" + b);
	}
}

for obj i use

public class Obj_Obj <E>{
	public E Obj;
	public Obj_Obj(){}
	public Obj_Obj(E obj){set(obj);}
	public E get(){return Obj;}
	public void set(E obj){Obj = obj;}
	public String toString(){
		return new String("" + Obj);
	}
}

Up:

You can change memory that use Java and all that fluffy things with pointer memory :wink:
its called Direct buffers

http://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html
allocate mem and then play with unsafe, - get mem address place allocated, and change values there)
but dont try move ther JAVA objects - i try its funny but crapy

Objects become out of mem space GC JVM and when GC called JVM try delete them
or relocate them when mem usage grows
so data stay same but objects marks as deleted
and when you try use them JVM crashes =)

it have some tricks for using obj, but need prevent all this crashes :wink:

SEE: handle vs. pointer. (and that reminded me that I typed the wrong thing above)