Please enable JavaScript to use CodeHS

C++ for Java Users

This tutorial covers the basics of C++ for users that are familiar with Java. It is not an exhaustive list of the differences.

By David Burnham

Welcome to C++! This tutorial is an overview of some basic C++ commands for people that have previously coded in Java. It is far from an exhaustive list of differences, but if you are familiar with Java, you should get enough to get up and running.

Java vs C++

At a high level, C++ is more of a blend between object-oriented programming and functional programming, while Java is really a pure object-oriented programming language. What does that mean? In Java, all of your code is contained inside a class and executed within methods/functions. C++ still has classes and functions, but also has code that lives outside of this structure. You can do a lot more in your main C++ file compared to what you would do with Java.

Java was developed with a syntax styled from C/C++, so you will find many similarities, especially with the basics. C++ does allow more lower-level programming compared to Java, but this tutorial will not go to that level of detail.

Console Input and Output

Here is a C++ example of “Hello World”. In C++, printing is done by essentially streaming output to the console.

cout << "Hello World" ;
Plain text

cout represents streaming to the console output and the <<indicates that you are sending output to the console.

In contrast, reading input from the console into a variable is done with the cin command and >>.

cin >> myVariable;
Plain text

You can find more details on console input and output in this tutorial.

Basic Program Structure

Just like in Java, C++ uses the main function, but the similarities in program structure stop there. Unlike Java, the main function does not reside in a class, but rather is a separate function in the program file. In addition, the main function traditionally does not take any parameters and it returns an integer (typically 0). Finally, while both Java and C++ have a default visibility of public, in Java it is usually stated for clarity, while it is seldom used in C++.

Like Java, C++ also relies on import statements, using the #include syntax, but they are more often required in C++. For example, if you want to do basic user input, you need to include iostream. Additionally, C++ relies on using namespaces to differentiate commands that may have the same name in different namespaces. To help simplify programs, a general std namespace is often included so that each command does not have to include the namespace declaration.

Check out the full example of Hello World below.

What is the same?

Many things in C++ use exactly the same syntax and basic functionality as you see in Java. Here is a quick run down on some of these similarities.

Loops:

for (int i = 0; i < 10; i ++)  {
    ...
}
Plain text

Conditional statements:

if (x < 10)  {
    ...
}
else if (x < 20) {
    ...
}
else {
    ...
}
Plain text

Most variable types:

int myInt = 10;
double myDouble  = 10.10;
char myChar = 'a';
Plain text

What is different?

In addition to the input/output and the program structure mentioned above, there are a few notable differences.

Boolean variables work similar to Java, but the name of the variable is shortened to bool and when printed, true values print as 1 and false prints as 0. You can still assign true/false, but they are printed with 1/0.

Strings variables use a lowercase ‘s’, string. The bigger difference here is that strings in C++ are mutable, which means the string itself can be altered without having to completely reassign it like you need to in Java. Find more information on strings in this tutorial.

Functions

Functions in C++ work similarly to Java by default, but as mentioned above, they do not usually contain the public declaration.

int add5(int num) {
    return num + 5:
}
Plain text

Just as is the case in Java, the default is to pass parameters by value, which means a copy of the variable is made for use in the function. C++ does give an option to pass by reference and you can read more about it here.

Here is an example program in C++. Can you spot all the similarities and the differences?