Main Function
C++
1
2
3
4
5
// free-floating function
int main( int argc, char* argv[])
{
printf( "Hello, world" );
}
Java
1
2
3
4
5
6
7
8
9
10
// every function must be part of a class; the main function for a particular
// class file is invoked when java <class> is run (so you can have one
// main function per class--useful for writing unit tests for a class)
class HelloWorld
{
public static void main(String args[])
{
System.out.println( "Hello, World" );
}
}
Compiling
C++1
2
3
4
// compile as
g++ foo.cc -o outfile
// run with
./outfile
Java1
2
3
4
5
// compile classes in foo.java to <classname>.class
javac foo.java
// run by invoking static main method in <classname>
java <classname>
Comments
Same in both languages (// and /* */ both work)
Class Declarations
Almost the same, but Java does not require a semicolon
C++1
class Bar {};
Java1
class Bar {}
Method Declarations
Same, except that in Java, must always be part of a class, and may prefix with public/private/protected
Constructors and destructors
Constructor has same syntax in both (name of the class), Java has no exact equivalent of the destructor
Static member functions and variables
Same as method declarations, but Java provides static initialization blocks to initialize static variables (instead of putting a definition in a source code file):
1
2
3
4
5
6
class Foo
{
static private int x;
// static initialization block
{ x = 5; }
}
Scoping static methods and namespaces
C++If you have a class and wish to refer to a static method, you use the form Class::method.
1
2
3
4
5
6
7
8
class MyClass
{
public:
static doStuff();
};
// now it's used like this
MyClass::doStuff();
JavaAll scoping in Java uses the . again, just like accessing fields of a class, so it's a bit more regular:
1
2
3
4
5
6
7
8
9
10
class MyClass
{
public static doStuff()
{
// do stuff
}
}
// now it's used like this
MyClass.doStuff();
Object declarations
C++1
2
3
4
5
// on the stack
myClass x;
// or on the heap
myClass *x = new myClass;
Java1
2
// always allocated on the heap (also, always need parens for constructor)
myClass x = new myClass();
Accessing fields of objects
C++If you're using a stack-based object, you access its fields with a dot:
1
2
3
4
5
6
7
// If you're using a stack-based object, you access its fields with a dot:
myClass x;
x.my_field; // ok
// But you use the arrow operator (->) to access fields of a class when working with a pointer:
myClass x = new MyClass;
x->my_field; // ok
JavaYou always work with references (which are similar to pointers--see the next section), so you always use a dot:
1
2
myClass x = new MyClass();
x.my_field; // ok
References vs. pointers
C++1
2
3
// references are immutable, use pointers for more flexibility
int bar = 7, qux = 6;
int& foo = bar;
Java1
2
3
4
5
6
// references are mutable and store addresses only to objects; there are
// no raw pointers
myClass x;
x.foo(); // error, x is a null "pointer"
// note that you always use . to access a field
Inheritance
C++1
2
class Foo : public Bar
{ ... };
Java1
2
class Foo extends Bar
{ ... }
Protection levels (abstraction barriers)
C++1
2
3
public:
void foo();
void bar();
Java1
2
public void foo();
public void bar();
Virtual functions
C++1
virtual int foo(); // or, non-virtually as simply int foo();
Java1
2
public void foo();
public void bar();
Abstract classes
C++1
virtual int foo(); // or, non-virtually as simply int foo();
Java1
2
// functions are virtual by default; use final to prevent overriding
int foo(); // or, final int foo();
Memory Management
Roughly the same--new allocates, but no delete in Java since it has garbage collection.
NULL vs. null
C++1
2
// initialize pointer to NULL
int *x = NULL;
Java1
2
3
// the compiler will catch the use of uninitialized references, but if you
// need to initialize a reference so it's known to be invalid, assign null
myClass x = null;
Booleans
Java is a bit more verbose: you must write boolean instead of merely bool.
C++1
bool foo;
Java1
boolean foo;
Const-ness
C++1
const int x = 7;
Java1
final int x = 7;
Throw Spec
First, Java enforce throw specs at compile time--you must document if your method can throw an exception
C++1
int foo() throw (IOException)
Java1
int foo() throws IOException
Arrays
C++1
2
3
4
5
6
int x[10];
// or
int *x = new x[10];
// use x, then reclaim memory
delete[] x;
Java1
2
3
int[] x = new int[10];
// use x, memory reclaimed by the garbage collector or returned to the
// system at the end of the program's lifetime
Collections and Iteration
C++Iterators are members of classes. The start of a range is <container>.begin(), and the end is <container>.end(). Advance using ++ operator, and access using *.
1
2
3
4
5
6
7
vector myVec;
for ( vector<int>::iterator itr = myVec.begin();
itr != myVec.end();
++itr )
{
cout << *itr;
}
JavaIterator is just an interface. The start of the range is <collection>.iterator, and you check to see if you're at the end with itr.hasNext(). You get the next element using itr.next() (a combination of using ++ and * in C++).
1
2
3
4
5
6
7
8
9
10
11
12
ArrayList myArrayList = new ArrayList();
Iterator itr = myArrayList.iterator();
while ( itr.hasNext() )
{
System.out.println( itr.next() );
}
// or, in Java 5
ArrayList myArrayList = new ArrayList();
for( Object o : myArrayList ) {
System.out.println( o );
}