Pointer Model that Java has over C++
Pointer Model that Java has over C++
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?
Answer by Scott Hunter for Pointer Model that Java has over C++
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.
Answer by Moshe Rabaev for Pointer Model that Java has over C++
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.
Answer by Apoorva sahay for Pointer Model that Java has over C++
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.
Answer by james large for Pointer Model that Java has over C++
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.
Answer by Adrian Jaoszewski for Pointer Model that Java has over C++
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.
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment