Re: some personal rambling on java the lang
On 10/21/2010 02:58 AM, George Neuner wrote:
What Java lacks that limits its utility with respect to C or C++ is
the ability to overlay a logical view of structured data at an
arbitrary memory location. This ability is the single feature of C
and C++ which makes them strictly more powerful than Java.
This doesn't work as well in C/C++ as you think. Specifically, in:
union {
int a;
double b;
} x;
x.a = 5;
The value of double is undefined. And let's not get into type punning.
I have occasionally desired union types in Java, but never really union
types.
First, there is no way *reliably* to obtain an arbitrary location
mapping on memory. Not all systems will support such mappings and
even if a particular platform does support it (e.g., Linux's /dev/mem,
/proc/<ID>/mem, etc.) there may be in place security protocols in the
JVM and/or host system which will prevent it.
How can Java be more powerful than what its host system provides? You
could probably do nio on /dev/mem, e.g., for Linux, though.
In any case, I have found little reason to do this, even in C or C++.
About the closest I can think of is mmap'ing binary files for
performance--nio can do that--or perhaps easier binary I/O, in which
case Java's Object{Input,Output}Stream is sufficient for my
serialization needs.
In contrast, both C and C++ do guarantee the order of data fields in a
struct. Field alignment padding is implementation dependent, but
virtually all compilers allow unaligned/unpadded structs if desired.
Both C and C++ provide the standard macro offsetof() for determining
the offset of a data field from the beginning of the structure. Thus
you could directly access a byte buffer containing an instance of a
struct without overlaying a view of the struct on the buffer.
offsetof has limitations. Specifically, offsetof does not work on
non-POD classes. Which was the first and only time to date I have needed
it [1].
You also forgot about the implementation-dependent issue of endianness.
So what? C++ has this feature called a "library" that, among other
things, allows you to use functions that you didn't write.
So does Java.
Among the many "libraries" that are available for C++ there are ...
OMG! Garbage Collectors ?!? Not refcounting hacks ... real
collectors from names you might actually recognize - that is if you
know anything at all about GC.
Garbage collection is not built in, and I do recall there being some
issues with garbage collection in C++, especially when you do large,
multithreaded programs. One program I worked on attempted to convert
from refcounting to garbage collection and failed.
There are languages that offer power fully equivalent to C++ but which
are cleaner, safer and easier to use. Some of them are OO, some are
functional. Almost all have built-in GC.
Java is not among them.
All I am trying to say is that a language doesn't need to do everything.
Also, if such languages exist, why does almost no one use them?
[1] Specifically, I was trying to write a C++ bridge library for dynamic
interlanguage trampolining, and needed offsets to the fields of a class,
so my use case totally involves non-POD classes.
--
Beware of bugs in the above code; I have only proved it correct, not
tried it. -- Donald E. Knuth