Chris Gregory

Home Blog LinkedIn Github

Java to C++ - Hello World

9/21/2015

What is the difference between C++ and Java? People experienced with both languages will give you a list of their favorite features not available in the other. But it doesn't really matter. What I care about are the features that make it so that my code makes sense. Maybe not as readable at first, but makes sense once you understand it. Lets look at some simple Java code then translate it to C++:

// Java
public class Begin {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}
// C++
#include <iostream>
int main() {
    std::cout << "Hello World!" << std::endl;
}

This code is honestly fairly similar. The main difference is that printing is done by using the overloaded << operator of the global variable std::cout. Overloading this operator makes the code much more understandable: just push the bits from the "Hello World!" string into the output. You will also notice that you don't need to have a class wrapped around the main statement for it to work! C++ has objects, they look and work very similar to those in Java, but it discourages you from using them when it doesn't make sense to do so.

You'll also notice the weird #include line. This informs the compiler that you want to use the functions provided by the iostream header provided in the standard library. It literally means that the compiler will just substitute in the entire file at that point then actually compile the program.

C++ really isn't very complicated compared to Java, it just does things differently and faster.