Jack said:
Java avoids the memory access violation suffered by C/C++. But it
still has the null pointer issue. Certainly, this makes Java code
safer than C/C++ code. But you can control memory directly with C/C++.
Is there any other advantages of Java on this perspective?
Also, the lack of direct memory control in Java means it's really hard
to mess up memory allocation.
I haven't played with C++ in a while, but there's certainly some darn
weird things you can do with memory allocation in that language. The
guys who program in C++ all the time seem to love it, but to me it just
looks like a opportunity to make some really serious errors and write a
bunch of byte where they shouldn't go.
In Java, your options are much more limited, but also much safer. You
can allocate memory with new (which cannot be overloaded like C++):
Object object = new SomeObject();
or you can allocate contiguous buffers with arrays:
byte[] buffer = new byte[256];
And fundamentally that's all I can think of. Both these objects are
subject to garbage collection, so there's no destructors to worry about,
all Java methods are virtual by default, and all objects are referred to
by reference, so copy constructors are not needed (or available, really).
All this makes it much easier to do "real" object oriented programming
in Java, imo, rather than have to worry about a lot of little low level
methods or deal with memory de-allocation on a class-by-class basis,
something I always found tedious and error-prone in C++.