Monday, April 16, 2007

C++ and Java Syntax Differences Cheat Sheet

main function
C++// free-floating function
int main( int argc, char* argv[])
printf( "Hello, world" );
Java// every function must be part of a class; the main function for a particular
// class file is invoked when java 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" );
C++ // compile as
g++ -o outfile
// run with

Java // compile classes in to .class
// run by invoking static main method in

CommentsSame in both languages (// and /* */ both work)
Class declarationsAlmost the same, but Java does not require a semicolon
C++ class Bar {};

Java class Bar {}

Method declarationsSame, except that in Java, must always be part of a class, and may prefix with public/private/protected
Constructors and destructorsConstructor has same syntax in both (name of the class), Java has no exact equivalent of the destructor
Static member functions and variablesSame as method declarations, but Java provides static initialization blocks to initialize static variables (instead of putting a definition in a source code file): class Foo
static private int x;
// static initialization block
{ x = 5; }
Object declarations
C++ // on the stack
myClass x;
// or on the heap
myClass *x = new myClass;

Java // always allocated on the heap (also, always need parens for constructor)
myClass x = new myClass();

References vs. pointers
C++ // references are immutable, use pointers for more flexibility
int bar = 7, qux = 6;
int& foo = bar;

Java // references are mutuable and store addresses only to objects; there are
// no raw pointers
myClass x;; // error, x is a null ``pointer''
// note that you always use . to access a field

C++ class Foo : public Bar
{ ... };

Java class Foo extends Bar
{ ... }

Protection levels (abstraction barriers)
C++ public:
void foo();
void bar();

Java public void foo();
public void bar();

Virtual functions
C++ virtual int foo(); // or, non-virtually as simply int foo();

Java // functions are virtual by default; use final to prevent overriding
int foo(); // or, final int foo();

Abstract classes
C++ // just need to include a pure virtual function
class Bar { public: virtual void foo() = 0; };

Java // syntax allows you to be explicit!
abstract class Bar { public abstract void foo(); }
// or you might even want to specify an interface
interface Bar { public void foo(); }
// and later, have a class implement the interface:
class Chocolate implements Bar
public void foo() { /* do something */ }

Memory managementRoughly the same--new allocates, but no delete in Java since it has garbage collection.
NULL vs. null
C++ // initialize pointer to NULL
int *x = NULL;

Java // 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;

BooleansJava is a bit more verbose: you must write boolean instead of merely bool.
C++bool foo;
Javaboolean foo;
C++ const int x = 7;

Java final int x = 7;

Throw SpecFirst, Java enforce throw specs at compile time--you must document if your method can throw an exception
C++int foo() throw (IOException)
Javaint foo() throws IOException
C++ int x[10];
// or
int *x = new x[10];
// use x, then reclaim memory
delete[] x;

Java 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 .begin(), and the end is .end(). Advance using ++ operator, and access using *. vector myVec;
for ( vector::iterator itr = myVec.begin();
itr != myVec.end();
++itr )
cout << *itr;

JavaIterator is just an interface. The start of the range is .iterator, and you check to see if you're at the end with itr.hasNext(). You get the next element using (a combination of using ++ and * in C++). ArrayList myArrayList = new ArrayList();
Iterator itr = myArrayList.iterator();
while ( itr.hasNext() )
System.out.println( );
// or, in Java 5
ArrayList myArrayList = new ArrayList();
for( Object o : myArrayList ) {
System.out.println( o );

TemplatesThis is still being to be added. See for a good introduction.

No comments: