1

I have read there is no pointer concept in Java, however I have also read several times that this is a keyword in Java that refers to the current object. I am still confused as to whether this can be called as a pointer or whether it's not a pointer at all.

If it's not a pointer, how can it refer to an object? How can it keep a reference of the current object? I have read that for C++, this is a pointer that holds the address of the current object.

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
ScoRpion
  • 11,364
  • 24
  • 66
  • 89

3 Answers3

5

Yes, every object-type variable in Java is in fact a pointer, but we call it a reference, most of the time. It's still a pointer, since if it's null and you dereference it, you get a NullPointerException.

There are pointers in Java, but there is no pointer arithmetic. You can not increment a pointer like you would do in C for example.

Also : JavaScript and Java are two very different languages. Your question shouldn't be tagged javascript.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • 1
    No, they're not pointers. There may be some subtle disagreements on the topic, but most people define pointers in terms of memory addresses, and would say that they are a simple implementation of the concept of a reference. Java does references. – CPerkins Oct 15 '11 at 15:36
  • 3
    @CPerkins : The reference for Java is not what most people think, but what the JLS says. And section 4.3.1 says : "The reference values (often just references) are *pointers* to these objects, and a special null reference, which refers to no object. The index says for "pointers" : See references. They are synonyms in the JLS. – JB Nizet Oct 15 '11 at 17:25
  • Another wrong answer accepted as correct. This seems to be common during the weekend. – Steve Kuo Oct 16 '11 at 01:47
  • 1
    @Steve Kuo: *you* might think my answer is wrong, but the Java Language Specification says it's correct. See section 4.3.1. What's the most credible? Your opinion, or the JLS? – JB Nizet Oct 17 '11 at 06:49
2

While all objects in Java are actually object references, Java has no such term as pointer, as it is a higher level language, this means that although this refers to the current object, there is no use of its actual value as a reference. Also, it might be only a handle, to the object, and not a pointer to the actual location of the object in memory. (Unlike any implementation of this in C++ that I'm aware of, where this points to the allocated memory of the object)

MByD
  • 135,866
  • 28
  • 264
  • 277
0

it is not a pointer but just a reference which is not dynamically by use.

manish
  • 1