Introduction to RePast: Extras


This page just outlines, very briefly, some Java bits not covered in the core programming module that are used in the RePast tutorial.


Generics

Generics are a extra structural option added with Java 1.5/5. They alter the general storage classes in Java, the ones that store java.lang.Object objects, so that they can be constrained to only use more specific classes, thus:

Vector<String> v = new Vector<String>;

If we do this and try to add anything other than a String to v we'll get an exception, if it compiles at all. Again, this is an example of a good language constraining you so you can't make mistakes, and you'd be well advised to use this form whenever you have a storage object that takes in very general objects. If nothing else, it means you don't have to cast objects when you get them out of the store, e.g.:

String s = v.next();

Rather than...

String s = (String)v.next();

There is nothing to stop you constraining to types that are themselves constrained, e.g.

Vector<List<String>> v = new Vector<List<String>>;

In the documentation, you'll see any class that allows this has <T> in its class definition (and possibly also some methods).

You can find out more about Generics at this Java Tutorial.


Annotations

Annotations are comments that can be interpreted and acted upon by the compiler/JVM. Again, they were added in Java 1.5/5. They start with the "@" symbol. You can write your own annotations and handlers for these, as the RePast team has. There are also some standard annotations, like the @Override annotation that Arc plugins use.

You can find out more about Annotations at this Java Tutorial.


For-each loops

The For-each loop allows you to run through collections/arrays of objects/primitives, dealing with each one and a time. Again, it was added in Java 1.5/5. It looks like this:

for (ClassType variable : collectionOrArrayVariable) {
   // Do stuff with variable
}

You can find out more about For-each loops at this Java Tutorial and this explanation of some of their quirks.


Other Java 1.5 additions

1.5 actually added quite a lot of syntax and structures to the language, bringing it up to date with more recent languages that took the Java ideas and improved on them. You might be interested in:

Enum types

... varargs

Autoboxing


instanceOf

Finally, a reminder of a piece of core Java we didn't cover in the Core module: the instanceOf operator. This allows you to compare an object with a class type, thus:

if(ourObject instantOf ClassType) {

It isn't a very fast bit of code, so you don't want to use it where speed is of the essence, but it is useful. It generates a boolean as an answer.

You can find out more about the instanceOf operator at this Java Tutorial.