2

The single biggest difference between Java and C/C++ is that Java has a pointer model that eliminates the possibility of overwriting memory and corrupting data.

This is a quote from a textbook. I always assumed that it was C++ that used pointers and not Java. Can someone please elaborate on this?

Sartaj
  • 29
  • 1
  • 6
    Of course Java has pointers; otherwise, how would you get NullPointerExceptions? – Klitos Kyriacou Aug 19 '16 at 18:34
  • 1
    Book is saying Java has a pointer **model**, not a pure pointer! The word model in English means a representation. You can't create direct pointers, though behind the scene there are pointers throughout the implementation. – Am_I_Helpful Aug 19 '16 at 18:34
  • 1
    The main difference is that, in Java, you can't do arithmetic on pointers. – Klitos Kyriacou Aug 19 '16 at 18:36
  • The book means to explain the concept of using references, both languages allow you to manage this, java doesn t allow aritmetic operations with pointers... – ΦXocę 웃 Пepeúpa ツ Aug 19 '16 at 18:36
  • 1
    Java [does not have pointers](http://stackoverflow.com/questions/2629357/does-java-have-pointers) (as the word is used in C++ as least), it has references. – resueman Aug 19 '16 at 18:36
  • there is nuance to how C/C++ handles pointers and how java does it. – Acewin Aug 19 '16 at 18:37
  • though it says NulllPointerException in its just usage terminology. IMO it simply means there is no no value assigned not like how C/C++ manages it. in C/C++ you can directly access a memory if u know the memory through pointer but you cannot do that in Java. Because you never know the memory address – Acewin Aug 19 '16 at 18:43
  • C/C++ has pointer and pointer variable. where as java has only pointers but no pointer variable – Acewin Aug 19 '16 at 18:56

5 Answers5

5

I would argue against ever using the word "pointer" in describing a Java program. I always say "object reference" (or just "reference") instead.

A pointer, in C or C++, identifies a location in the process's virtual address space, and the virtual address space basically is a giant, typeless array. Given a pointer, you can add an offset to it, and get a pointer to a different location in the array. Given two pointers, you can compute the offset between them.

You can't do any of that with Java object references. A Java object reference identifies an object on the heap. The address of that object can (and probably does) change from time to time, but it's identity always is unique.

You can't "add an offset" to an object reference and get a different object reference. It doesn't make any sense.

Solomon Slow
  • 25,130
  • 5
  • 37
  • 57
  • I too go by your answer. though it says NulllPointerException in its just usage terminology. IMO it simply means there is no no value assigned not like how C/C++ manages it. in C/C++ you can directly access a memory if u know the memory through pointer but you cannot do that in Java. Because you never know the memory address – Acewin Aug 19 '16 at 18:44
  • this is going to come down to the concept as why java works on pass by value and never pass by reference. – Acewin Aug 19 '16 at 18:46
  • @Acewin, the question of pass-by-value vs. pass-by-reference is independent of whether or not the language has C-style pointers. – Solomon Slow Aug 26 '16 at 12:44
2

They both use pointers.

In C++, pointers are actual memory addresses, meaning you can use one (via pointer arithmetic) to identify arbitrary memory locations.

In Java, pointers are opaque: knowing the value of one doesn't give you any information about anything other than the object it is pointing to.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
  • 4
    A JVM might use pointers in its implementation, but "pointer" is not really a concept in the Java language itself. (Yeah, I know, the word "pointer" appears in the name of the `NullPointerException` class, but IMO, that's just one of many names in Java that were not well thought out by the original authors.) – Solomon Slow Aug 19 '16 at 18:39
  • `NullReferenceException` would have been better. – Kishore Aug 19 '16 at 18:41
1

C++ uses pointers/references and it's the programmers responsibility to acquire and release dynamic memory allocations. Java also does internally but in order to keep track of memory java uses a reference count which is in most simplest terms the evil Garbage Collector! Java hides the pointers from the programmer and C++ doesn't thus making C++ far more powerful, but also dangerous. With Java you cannot allocate user defined objects on the stack (only primitives like int...) but in C++ you can use new keyword to allocate a user defined object on the heap or on the stack.

Moshe Rabaev
  • 1,892
  • 16
  • 31
1

Javas "reference system" is its pointer system. They're basically pointers without the possibility of casting to an incompatible type. It also does not allow you to use pointer arithmetic and like C and C++ do. In C++ you can corrupt your data by:

class SomeClass {
private:
    int a;
public:
    SomeClass(int a): a(a) {}
};

Later you can corrupt an object of the class by basically doing this:

SomeClass victim(2);
unsigned char *hazardousPointer = (unsigned char *) &victim;
for (int i = 0; i < sizeof(SomeClass); i++)
    hazardousPointer[i] = 0;

This piece of code just allowed us to violate the private access rights and allowed to change its state (it could be also const, that does not matter). This happened due to the fact that C/C++ pointers are just memory addresses which pretty much behave like integers with some restrictions. Furthermore hazardousPointer[i] is just C/C++ syntactic suggar for *(hazardousPointer + i) which is not the case with Java, where arrays are objects which can even be returned from methods and have their own methods.

Furthermore Java has a garbage collector which cares about memory leaks, where C++ relies on its system stack and wrappers or smart pointers.

Community
  • 1
  • 1
Adrian Jałoszewski
  • 1,695
  • 3
  • 17
  • 33
0

In java everything is references. so you can create as many aliases of a variable and if you update any one of it, it will be reflected in all the alias variable.

On the other hand, C++ has pointers and references both.

Apoorva sahay
  • 1,900
  • 3
  • 28
  • 45