Java object references are more like pointers than they are C++ objects.
Yea. The main difference is that C++ doesn't give you a NullPointerException() automatically when you attempt to use a null pointer. In fact, if you attempt to use a bad pointer, the results are "undefined" (a specification you'll see often in C++ and C). Finally, C and C++ give you the concept of pointer arithmetic.
for(char *p = "hello"; *p; ++p) {
putc(*p);
}
This snippet just prints out "hello" to the console (without a carriage return). What this snippet essentially does is to allocate a null-terminated C string in the data segment of your application's executable. The "null-terminated string" means simply that the ascii bytes of the text will appear in memory followed by a zero value to indicate the end of the string of characters. So I assign a char pointer named "p" to the address of the first character in this string and I iterate through the loop for as long as the value at the address pointed to by p is non-null (the expression "*p" is the same as converting the value "*p" to a boolean that's false if zero and true if non-zero). Finally, by saying "++p", I'm using pointer arithmatic to increase the pointer. The pointer is increased by the size of the object type it's pointing to. In this case, a "char" is only one byte in size.
If this were "int *p = {5, 4, 3, 0};" instead, then each time I call "++p", the pointer will increase by 4 since that is the size of an int (at least if you're using either 32 bit or 64 bit C/C++, in 16-bits, it was 2 bytes in size -- or the "long" type was, I forget now!).
So in short, C and C++ are much dirtier than Java. Java is a pure object oriented language, something very few other languages these days can boast. This is simply because everthying derives from java.lang.Object. Perhaps C# could claim that if it wasn't written by m$, but that very fact (in my heart and mind) immediately causes me to dismiss it as a very "impure" entity
. So anyway, everything (except for primitives) in Java is an object. C++ actually has no real distinctions between objects and primitives. This, each of these is considered a call to a constructor:
int *j = new int(5);
MyClass *o = new MyClass(some, parameters);
// or, using an uglier "create a temporary object and then use an assignment operator" approach
int j = int(5);
MyClass o = new MyClass(some, parameters);
// and finally, the "just initialize the object using the constructor I specify" approach:
int j(5);
MyClass o(some, parameters);
The difference being that "int" is a built in type.
Disclaimer: I haven't actually tested this code, but I believe it's valid.
Many of the shortcomings of C++ have been getting filled in here and there over the past decade or so in a most quaint fashion. Overall, if I had to compare the two, I would say that the primary difference these days is that Java still uses more memory (it's designed to) and in some areas, offers inadequate performance guarantees while C++ is still, at it's core, ugly
. But C & C++ is a statically compiled language (there are a few dynamic optimization runtimes out there, but they are experimental) while Java is dynamically compiled. This means that Java programs run slower when they start, but that they have both the data and the code when they are given an opportunity to compile and by analysing how functions are being used (when they get used a lot) it can compile multiple optimized versions of those functions specific to the data sets that it's being called with, thereby providing optimization opportunities that do not exist in statically compiled languages. However, that also means that it runs slower at first and performance increases as you run the app. With C and C++, the performance is almost identicle at both start and later in it's execution (except when data has to be loaded from the disk and is later cached by the OS's kernel).
I'm a recent student of Scott Meyer's
Effective C++ book (there's two more books of his I need to get as well,
More Effective C++ and
Effective STL). These books are not intended for the C++ initiate, but I would recommend them as soon as you begin to feel comfortable with the language.
Finally, the new C++ specification is to be finalized this year, called C++0x (the 0x is because it was supposed to be released sometime in the 1st decade of 2000, but before 2010 -- as it turns out, just slightly before 2010!). This introduces a mass of new language features that I'm SO behind on that it's just silly!