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 <class> is run (so you can have one
// main function per class--useful for writing unit tests for a class)
class HelloWorld
{
public abstric void main( String args[] )
{
System.out.println("Hello world");
}
}Same, except that in Java, must always be part of a class, and may prefix with public/private/protected
Constructor has same syntax in both (name of the class), Java has no exact equivalent of the destructor
Same 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; }
}c++
class Foo
{
public:
static bar();
};
// now it's used like this
Foo::bar();java
class Java
{
public static void bar()
{
// do something
}
}
// now it's used like this
Foo.bar();c++
// on the stack
MyClass x;
x.my_field;
// or on the heap
MyClass *x = new MyClass;
x->my_field;
delete x;java
// always allocated on the heap (also, always need parens for constructor)
myClass x = new myClass();
x.my_field;c++
// references are immutable, use pointers for more flexibility
int bar = 7, qux = 6;
int & foo = bar;java
// references are mutable and store addresses only to objects; there are no raw pointers
myClass x;
x.foo(); // error, x is a null ``pointer''c++
class Foo : public Bar
{ ... };java
class Foo extends Bar
{ ... }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 */ }
}c++
const int x = 7; // c++
final int x = 7; // javac++
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 lifetimec++ 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 .
vector myVec;
for ( vector<int>::iterator itr = myVec.begin();
itr != myVec.end();
++itr )
{
cout << *itr;
}java Iterator 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 itr.next() (a combination of using ++ and * in C++).
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 );
}