Some Notes on C++ for Java Programmers

Here is a collection of Java/C++ differences we have discovered so far in this class. If, while working on your projects (or even projects in other classes) you have learned of some other differences, please e-mail them to one of the TAs so that it can be included here. I can also include notes which are not specific to Java/C++ differences but just good notes to have when programming in C++. Perhaps some of you more experienced in C++ can add some of these.

Note 1:

Pointers are not initialized to NULL. Therefore, if you expect a pointer to be NULL at some point, you need to make sure that you have set it to NULL.

Note 2:

The following lines are (approximately) equivalent.

In Java: Node r = nodes[i];
In C: Node* r = &nodes[i];

Therefore, if you intend for r to be the same node as nodes[i] which would occur automatically by typing the first line in Java, you must type the second line in C and use r as a pointer to nodes[i]. If you type

Node r = nodes[i];
in C, the Node r will be a different object than nodes[i] and thus any changes you make to r will not affect the Node nodes[i].

Note 3:

Division by 0 will not cause an exception in C. Therefore, if your program performs a divide by zero it will continue running until the result causes your code to crash likely somewhere far from the problem, thus making this difficult to debug.

Division by zero can often ocurr when dividing by a user entered value, normalizing a zero vector, normalizing a filter with weights summing to zero, ...

Note 4:

When using printf, it is necessary to use the correct type syntax for printing variables.

int i = 3;
float x = 2.0;
char t[] = "Sziasztok világ!"

printf(  "%d   %f   %s   %c  \n",   i,   x,   t,   t[i]);