C++ vs Java -- an example

C++ 
Java 
Comments 
// ISLList.cpp 
// Roberto Ordonez
// ISLList.java 
// Roberto Ordonez
Files should be named mainClassName.java 
Comments look exactly the same (except for javadoc comments: /** */)
#include <iostream.h> No header (.h) files to #include in Java--the compiler finds the necessary classes automatically 
All java.lang.* classes are automatically imported (available for use without full package name)
template <class T> No need to use templates--since all classes are derived from Object, a reference to an Object can hold a reference to any subclass, i.e., any object
class ISLNode 
{
class ISLNode 
{
Class declarations look almost identical
    public: No public/protected/private sections in Java--instead, each member is given a specific access level
        T *data;     public Object data; Instead of a pointer to a template class, use a reference to a (generalized) Object
        ISLNode<T> *next;     public ISLNode next; No pointers--variables of class type are always references to (dynamically allocated) objects in Java
        ISLNode() 
        { 
            data = next = NULL; 
        }
    ISLNode() 
    { 
        data = next = null; 
    }
The value null (which must be lowercase) is actually part of the Java language; in C++ it's only a #defined constant (that is why we have to #include <iostream.h>); also, in Java, methods must be defined within the class definition (you cannot have separate .h and .cpp files as in C++)
}; } No semicolon after class declarations in Java
template <class T>
class ISLList public class ISLList Classes have access specifiers (for access level within/outside the package) in Java (note that a public class must be in a file called className.java)

    protected: 
        ISLNode<T> *head, *curr;

    protected ISLNode head, curr;
Remember that variables of class types are actually references (like C++ pointers without the messy pointer notation)
    public: 
        ISLList(): head(NULL), 
            curr(NULL) { }
    public ISLList() 
    { 
            head = curr = null; 
    }
There is no special "constructor notation" in Java, since object are never constructed without an explicit call to a constructor
        ISLList<T>& operator<<=(T * 
            newData) 
        { 
            ISLNode<T> prev1, curr1, 
    public ISLList insertBefore(Object 
        newData) 
    { 
        ISLNode prev1, curr1,
No operator overloading in Java--you must use member functions instead
                new1 = new ISLNode<T> ;             new1 = new ISLNode(); Calls to constructors always require ()
            new1->data = newData;         new1.data = newData; No messy pointer notation--you always use the dot operator!
            if (!head) 
                head = new1; 
            else 
            { 
                prev1 = NULL; 
                curr1 = head; 
                while (curr1 != curr) 
                { 
                    prev1 = curr1; 
                    curr1 = curr1->next; 
                } 
                if (prev1) 
                    prev1->next = new1; 
                else 
                    head = new1; 
                new1->next = curr; 
            } 
            curr = new1;
        if (head == null) 
            head = new1; 
        else 
        { 
            prev1 = null; 
            curr1 = head; 
            while (curr1 != curr) 
            { 
                prev1 = curr1; 
                curr1 = curr1.next; 
            } 
            if (prev1 != null) 
                prev1.next = new1; 
            else 
                head = new1; 
            new1.next = curr; 
        } 
        curr = new1;
Java has a boolean type (with values true and false) which must be used in conditionals like if and while (you cannot simply use an integer as in the bolded C++ statements)
            return *this;
        }
};
        return this;
    }
}
No need to dereference pointers (ever)!
 
A few other important differences that are not immediately obvious from this example:             void quarterlyUpdates(Object obj)
            {
                if (obj instanceof Vehicle)
                    obj.checkInsurance();
            }