Java and C++ side by side

These pages are for programmers who write C++ and are learning Java. Each one takes a single task, writes it in both languages, and explains what differs and why: who owns the value, what the compiler checks, and what happens at run time.

The C++ side is shown but not compiled here: this image has no g++, so a C++ block on these pages carries the source and no output, and the text says what the program does. The Java side is built with javac and run, and its output is the output of the run. The interesting part is rarely the syntax: it is the habit that compiles in C++ and behaves differently in Java, and every page ends with one.

The pages are independent of the course: read one when its topic comes up in your work, and follow the links into the Java course and the C++ course for the full lesson.

Heap Memory and the garbage collector new and delete against the collector, a destructor against reachability, and the object that dies at a different time. Open Type Generics and templates Type erasure against instantiation per type, and the error that arrives at the call in one language and at the definition in the other. Open Text Strings An immutable String against std::string, the append that only C++ allows, and the comparison that is not ==. Open List Collections ArrayList and HashMap against vector and map, boxing, and the reference that a resize invalidates in C++. Open Throw Exceptions and errors Checked exceptions against throw specifications, finally against a destructor, and the error that is not an exception. Open Jar Build and packaging A jar with a manifest against an executable and a make rule, and what the two runtimes need to start a program. Open Thread Threads A Java thread over a shared object against std::thread, volatile, synchronized and the data race that Java defines. Open Bounds Undefined behaviour A bounds check and an exception against the access whose result is not defined, and the null that one language checks. Open