Hello World Java to C++

This is the classic "Hello World" program. It is intended as the simplest possible program to write.

If you need an introduction, there is a tutorial that will show you how to set up "Hello World" in Visual Studio.



*Something.java[2] means the file "Something.java" at line 2.


The main() method/function is very similar in Java[4] and C++[4]. All of the "public static void is eliminated and replaced with just a return value of int. In most cases you will not need to do anything with the return value. In C++ the main() function still has argument parameters, but if you are not using them you don't need to declare them.


In C++[1] there is the #include compiler directive. This serves a similar function to the import statement in Java. However, #include does not know anything about C++. it only knows to take everything from the iostream file and put it into the code at the position where the#include is found. Thus the C++ compiler would see a file that contained all of the iostream file followed by lines 2-8 of the Hello World.cpp file. Remember that #include is dumb. It only puts one file into another file. It does not know any C++.


A namespace in C++[3] is similar to a package in Java. In Java we would import a package so that we did not need to explicitly give the full name for everything in that package. In C++ we do using namespace. On C++[3] the name std is the namespace for standard input and output.

writing out information

Output in C++ uses the concept of streams that is similar to Java. On Java[6] System.out indicates that the printing is to go to the standard output stream. On C++[6] the printing is to go to the cout stream. System.out and cout are pretty much the same thing. The difference is that Java[6] uses the println() method and C++[6] uses the "<<" operator. In C++ one can override operators as well as methods. The C++ output stream has the "<<" operator that works the same as the print() method in Java. Also Java[6] uses the println() method which prints things out and then adds a new line to go to the next line. In C++[6] an additional "<<" operator is used followed by the endl constant. This accomplishes the same thing.


C++[7] has the additional system("pause"); statement. If you leave this off, when your program runs it will complete and then close the console window immediately. This can be very frustrating if you are trying to see what happened. The system("pause"); statement will pause the execution so that the window does not close and you can see the output. Java does not need this because it does it automatically at the end of the program.