CS 1124 — Object Oriented Programming

Java to C++

The purpose of this document is to help those with a background in Java, make the transition to programming in C++. It is specifically targetted at students taking CS1124 at Brooklyn Poly, who have either advanced placement or transfer credit from an introductory Java course.

Two things to note before we dive in:

A First Program

Let's start by looking at how the traditional "hello world" program might be written in Java and C++.

In Java:

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}

In C++:

#include <iostream>
using namespace std;
int main() {
    cout << "Hello world!\n";
}
     

So, how do these programs compare?

Basic Types

Java and C++ have similar built-in types.

Integers

Floating Point

Boolean

 

Reading a File

There are a number of ways to read files. The easiest in many cases is to use the class Scanner, which allows you to directly read in the various built-in types, along with lines and whitespace-delimited "words". Here is a simple program to read in a file of integers, displaying their sum and average.

In Java:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadFile {
	public static void main(String[] args) throws FileNotFoundException {
		Scanner scan = new Scanner(new File("nums.txt"));
		int tot = 0, n = 0;
		while (scan.hasNextInt()) {
			int num = scan.nextInt();
			tot += num;
            ++n;
			System.out.print(num + " ");
        } 
        System.out.println("T\notal: " + tot + "; Avg = " + tot/n);
	}
}

In C++, the code looks like:

#include <iostream>
#include <fstream>
using namespace std;
int main() {
    ifstream ifs("nums.txt");
    int tot = 0, n = 0;
    int num;
    while (ifs >> num) {
        tot += num;
        ++n;
        cout << num << " ";
    }
    cout << "\nTotal: " << tot << "; Avg = " << tot/n << endl;
}
 

Before we compare the code, we should note that neither program properly handles the situation in which we fail to open the file nums.txt. You could say that Java does a better job here, as it will crash the program by throwing a FileNotFoundException.

Now onto the code comparison. I won't discuss things that were already covered in our early code comparison.

 

Arrays

Arrays in Java and C++ have a number of similarities and also a number of differences.

Note that in both languages, it is common to instead use a more powerful and flexible class for holding groups of things. In Java that would normally be the ArrayList, while in C++ it is the vector. However, in simple cases arrays are still used in both languages, so they are worth comparing.

To highlight the basic use of arrays, let's take our earlier program that read a file of integers, summing them and computing their average. Here we will read the numbers into an array.

In Java:

int n = 0;
int[] arr = new int[10];
while (scan.hasNextInt()) {
    int num = scan.nextInt();
    arr[n++]= num;
} 
System.out.println("T\notal: " + tot + "; Avg = " + tot/n);

In C++, the code looks like:

int tot = 0, n = 0;
int num;
while (ifs >> num) {
    arr[n++]= num;
}
cout << "\nTotal: " << tot << "; Avg = " << tot/n << endl; 

The above code looks very similar. First let's examine it. Then we will explore further some of the major differences between Java's arrays and those of C++.

General Issues

Cat& c; // Illegal. Must say what c is a reference to.


Home


Maintained by John Sterling (jsterling@poly.edu). Last updated January 23, 2011