A few pointers for C++ students 


Those of you not familiar with java may find this page useful since you will need to program in Java for your project.

It is not necessary to know the vast library of classes that are available to most standard java implementations since the java api documentation is available on the web. Operating systems students will want to take a look at the Thread class


Some important differences between Java and C++:

Java was designed with syntax similar to c++ so that it would be fairly easy to learn for C++ programmers. Unfortuately there are just enough peculiarities that the transition from one to the other takes some work. Because so much is the same between the two languages, I'll only concentrate on those things that strike me as being quite different. Feel free to ask any questions in class, during my office hours or by email.

Memory management:

In C++ for every object that you create on the heap (every object that you create with the new keyword) you must free up the memory and resources yourself using the delete keyword. Object that you create by simply declaring them are created on the stack and are cleaned up for you when they go out of scope.

In java, all objects are created using the new keyword and they are all created on the heap, however you do not have to do your own memory management. Java has a garbage collector that will clean up unused memory for you. You still need to clean up other resources in your class destructors though.

No C-functions (Not even main)

Unlike C++, there are no C-functions available in Java, every function is a method of some object or some class.
Static (class) methods do exist , these are the closest to C-functions that are available.
The main function is also a class function in Java. The prototype is
public static void main (String[] arg){
    //body of function here.
}
This method becomes part of one of your classes.

To run your program you will use lejos <your class that includes the main method>

Using other classes


Since there are no header files, you can't really use the #include preprocessor directive as in C++. Instead, one uses the java keyword 'import' to tell the compiler that this classes uses some other classes. You can import a single class using the import statement like:
import java.lang.System;

Or you can import an entire package (a group of classes that are related and are located in the same directory)  For example
 import josx.platform.rcx.*;
imports all of the classes in the josx.platform.rcx package. (Something you will probably do while you are building RCX programs.)

Classes


Everything is an object in Java. To drive that home, there is no seperation of class declaration in a header file and class definition in a code file the way htere is in C++. The class declaration and definition are one.  For example, in the RCX hello world example included in the lejos examples directory (class reproduced below), The main method in implemented right in the class declaration. All other classes are implemeted the same way.

public class HelloWorld
{
  public static void main (String[] aArg)
  throws Exception
  {
     LCD.clear();
     TextLCD.print ("hello");
     Thread.sleep(2000);
     TextLCD.print ("world");
     Thread.sleep(2000);
  }
}
Instance variables are included in the class difinition as usual, though they may appear below the methods/member functions that use them and still be syntactically correct, it is generally good form to put them at the top of the class.

It is important to note that to encourage good programming, each file can only contain one public class and that class must have the same name as the Java file name (without the .java extension)

Inheritance

There is no multiple inheritance in java. A class can only directly inherit from one class at a time. To do that the class uses the extends keyword. If a class doesn't explicitly extend another class, then it extends the Object class, which is a simple class that is the root of the Java class hierarchy.

Operating systems students will want to extend the Thread class for your lab and then use the synchronized keyword for

For example from the lejos examples directory
public class MotorView extends PortView //Class MoterView inherits from
                                         //class Portview

{
  static final int MAXSTATE = 3; //some constants
  static final int MAXPOWER = 7;

  <snip>
  int iState;
  int iPower;

  MotorView( Motor m, int n ) //constructor as one would expect
  {
    super( true, n);
    motor = m;

    iState = 0;
    iPower = 7;
    setState();
    setPower();
  }

<snip>
 public void showValues() //member function/ method as one would expect
  {
    LCDProgramNumber.set (iState);
    LCDNumber.set (iPower, 0, true);
  }

<snip>

};

Interfaces:

To get around the limits of multiple inheritance, java provides interfaces which can be thought of somewhat akin to pure virtual classes which have no member functions defined at all.

Interfaces are a list of capabilities that every class that implementes the interface must make available. Interfaces can be used as the type for formal parameters, return types,  variable definitions and most other places that a type must be used. You cannot however try to create an object of the interfact type. Rather, you create an object of the class that implements the interface and use that object where you need the interface. To implement an interface one must use the implements keyword and then implement all of the methods declared by the interface for example:

class Subsumption implements ButtonListener {
<snip>
    public void buttonPressed(Button b) {
        running=false;
    }
       
    public void buttonReleased(Button b) {
    }

};
The ButtonListener interface requires the two methods
buttonPressed and buttonReleased to be implemented to implement the interface. There are several interfaces in the lejos API that might be of interest.

Thats all for now.

These are the basic differences between Java and C++ that you are likely to run into in your early work on your project. Please let me know if you encounter any other oddities and I'll add them to this page.