Fred's Home Page

About me
csh is bad
Help out
KRoC/Linux (old)
occam upgrades
occam tutorial
Projects (PhD)
Quick gdb
UNIX crashcourse
WG / WGBuilder

A Simple Glossary of some Common Java Things

This page is not entirely complete yet, but is hopefully error-free. If you find anything wrong, misleading or missing, please let me know)

access modifier

An access-modifier refers to the public, private and protected keywords found before instance variables and methods. Briefly:

  • public: variable/method can be accessed outside the class
  • private: variable/method can only be accessed from within the class
  • protected: variable/method can only be accessed from within the package, and also through inheritance


An accessor is a method which is used to get the value of something. For example, the getHeight() method is an accessor. There is nothing special about accessors in Java terms, it's just a word used to describe what they do (access something).


An alias is, literally, a different name for the same thing. Aliases can only be created between object types (see data-type). Consider this code:

    Thing a, b;

    a = new Thing();
    b = a;

This declares two `Thing's, called `a' and `b', initially undefined. The first assignment creates a new Thing and assigns it to `a'. The second assignment assigns `a' to `b'. This does not create a new Thing, however. It makes `b' point at the same Thing that `a' points at. After the assignments, `a' and `b' are aliases for the same Thing. A change to the contents of `a' will be visible in `b' -- because they are the same object.

Though the above fragment does not demonstrate it, aliases can be useful for efficiency (e.g. allowing separate parts of a program access to some shared object). However, they can also destroy program logic.

Looking a bit deeper at Java, it becomes clear that at least some aliasing is endemic. The declaration "Thing a;" does not declare a "Thing" as such, but rather a reference to a "Thing" object. The "Thing" is created from "new Thing();" (using the new operator), which returns a reference to some new "Thing" object. Most often (and in the above fragment) the reference returned by "new" is assigned to some variable (not assigning it and just writing "new Thing();" is allowed, but in many cases is not useful).


Assignment is essentially the single-equals operator `='. The left-hand side of the assignment is always a variable. The right-hand side can be any valid expression.


An `attribute' refers to a variable declared inside a class. These are also known as ``fields'' and ``class variables''.

boolean expression

A boolean-expression is an expression which has a boolean (true of false) result. Common examples include the relational operators: ==, !=, <, <=, >, >=. Equality operators (`==' (equals) and `!=' (not equals)) work on objects, but test whether the two objects given are the same object (i.e. aliases for each other).


A class can be viewed in a couple ways:

  • As the .java file containing the class's contents (i.e. the file which starts proper with something like ``public class ...'')
  • As a collection of variables and methods inside a class XYZ { ... }

For example, one might refer to "the setHeight method in the Triangle class".

code block

A code-block is the code found between the left and right curly braces `{' and `}', in other words, a block of code. Code-blocks will generally be nested through the use of if-statements and for-loops.


A constructor is a special ``method'' within a class which is called when the class is instantiated with the new-operator. For example, a simple class might look like:

    class Foo {
        private int x;
        private int y;

        // constructor
        public Foo ()
            // initialise private variables
            x = 0;
            y = 0;

        // another constructor
        public Foo (int x, int y)
            // initialise private variables from parameters
            this.x = x;
            this.y = y;

        // so we can print() it
        public String toString ()
            return ("(" + x + "," + y + ")");

This class can be constructed (and used) in two ways:

    class Test1 {
        public static void main (String args[])
            Foo bar1, bar2;

            bar1 = new Foo ();
            bar2 = new Foo (42, 99);
            System.out.println ("bar1 = " + bar1);
            System.out.println ("bar2 = " + bar2);

As many constructors as required can be added to a class, although creating a vast amount of them in a single class is not recommended.

Note/update: Constructors are not methods in the sense of being methods, but it's probably the most accurate description for what it is (a chunk of code wrapped up with a name and optional formal parameters). Constructors look like methods, except they have no return type (not even `void'). If you add a return type, it becomes a regular method, and is no longer a constructor (this could be a potential source of errors..).

data type

Data-types, generally, provide a type-system for programming langauges. Types are a somewhat abstract concept -- in most cases, the actual hardware has very little knowledge of types (typically 8, 16, 32, 64 and 128-bit signed and unsigned integers; 32, 64 and 128-bit floating-point numbers; and addresses).

One way to think about types is as ``shaped holes'', with variables being the pieces that fit in those holes. When you declare a variable, you are effectively creating a new `piece'. The `holes' are created wherever a variable might be used. This includes type-casts, formal-parameters to methods (also shown in the method-signature), and operators.

java primitive-type shapes and holes The picture on the right tries to visualise this: imagine the three shapes represent variables of type `char', `int' and `double' in Java, that could be the code:

    char x;
    int y;
    double z;

The three `holes' could corresponding represent the formal parameters to a particular method, for example:

    public void foo (char a, int b, double c)
        // body of method

The process of putting these two together (putting the `shapes' in the `holes') would be the use of those three variables in a method-call to `foo', for example:

    foo (x, y, z);

In this case, the shapes clearly fit neatly. If the ordering of parameters is changed, however (typical programming error), the shapes will no longer fit in the holes and a compiler-error is generated.

java object-type shapes and holes The above shows an example of primitive-types. Object types are slightly more complicated, especially with inheritance, but fit this shape-analogy nicely. The picture on the right shows an example of three object `shapes' and three object `holes', for an `Object', `Integer' and `String':

    Object obj;
    Integer val;
    String str;

As above, these `shapes' can be plugged into their corresponding `holes' without a problem. But unlike that above, some of the `shapes' fit through different `holes'. The `String' shape will fit in the `Object' hole, but the `Object' shape will not fit in the `String' hole. This means, for example, that a `String' can be used as an actual-parameter to a method whose formal-parameter is an `Object'. This is allowed because `String' is inherited from `Object'. Similar rules apply to all the various object-types (classes) and their respective inheritance trees. In Java, any object is automatically inherited from `Object' -- so any object `shape' will fit an `Object' hole.

There is a certain element of `magic' in the Java type-system. This includes automatic type promotion for some primitive types and some fairly special handling for `String's. Automatic type promotion happens when, for example, the `String' shape is placed in an `Object' hole -- it automatically fits. The string-handling magic is a bit more subtle: if the compiler expects a string, and is given an object, it will use the `toString' method of that object, if present. primitive-types don't have methods -- the conversion to a `String' for these is just magic..

There is also one shape that cannot exist -- `void'. This is generally only used to explicitly indicate `nothing'.


A declaration is used to declare a variable of a certain type. To save typing, more than one variable can be declared in a single declaration, by seperating the variables with commas. For example:

    int x, y, z;
    String some_string;
    Board area;
    Triangle tri1, tri2;
    byte some_byte;

Declarations can appear at any point in code, making the declared variable(s) available from the declaraction to the end of the code-block (closing brace).


Encapsulation is essentially collecting together variables and methods inside a class. Classes are generally built in such a way that they model one particular aspect of a problem. For example, the "Triangle" class is used to represent a triangle, providing methods which allow the programmer to interact with a Triangle object. The implementation of a class (code and instance-variables inside it) should be transparent to the programmer (unless said programmer wrote the class). Only the thing a programmer needs to know about a class are its public methods and constructors.


Exceptions are a way of indicating failure inside Java programs. Exceptions are thrown by a method or code-block, and caught `higher-up' in the program. In order to catch an exception, a `try/catch' block must be used. This is a special construct, that attempts to execute some code, and if that code generates an exception, execution continues at the place where the exception is caught. This implies that if an exception is thrown, but is not caught, the program/thread will terminate -- and that is exactly what happens.

The actual `execption' thrown is an instance of a class that either extends `Exception' or implements the `Throwable' interface. This is more useful for creating your own exceptions, rather than just using them.

Any method that throws an exception must declare it as part of its method-signature. There is a slight exception to this in that any code can throw the special `RuntimeException', or anything decended (by inheritance) from it. `RuntimeException' can be caught, however. For example:

    public void foo () throws Exception
        Exception e;

        e = new Exception ("from foo()");
        throw e;

A corresponding `try/catch' block for this could be:

    try {
        System.out.println ("here 1");
        foo ();
        System.out.println ("here 2");
    } catch (Exception x) {
        System.out.println ("abort! " + x.getMessage());

When run, this code will not print out the second (``here 2'') message -- execution skips from the `throw' inside `foo' to the corresponding `catch' block. Multiple `catch' blocks are also permitted, enabling some selection over what exceptions are caught. As with the usual rules of inheritance and type compatability (see data-types), a `catch' block for a particular exception will catch that exception and any of its sub-classes. The ordering of `catch' blocks has an effect here, for example:

    try {
        foo ();
    } catch (Other o) {
        System.out.println ("Other: " + o.getMessage ());
    } catch (Exception e) {
        System.out.println ("Exception: " + e.getMessage ());

If `foo' throws an exception that is really an `Other', the first `catch' block will be used. For other exceptions, the second `catch' block would be used. This example, however, will not catch exceptions that do not extend the `Exception' class -- i.e. those that implement `Throwable'; those exceptions must be caught explicitly.


An expression is something which has a value. For example, "4 + 5" is an expression, as is "(((x + 1) / 10) - thing.getHeight())". Anything which is calculated is generally an expression. Expressions only make sense when dealing with primitive-types (boolean, int, float, double). One rare exception to this is String addition (which really means string concatenation).

flow of control

The flow-of-control in a program is the path traced during execution (running the program). Different statements affect the flow-of-control in different ways. For instance, when an if-statement is reached, the flow-of-control will either run through it (if the condition is true), or will skip it (if the condition is false).

for loop

A for-loop has the syntax "for (foo; bar; blip) { code }". foo is replaced with a statement which is executed before the looping starts. bar is replaced with a boolean-expression which determines whether code should be executed. blip is a statement which is executed at the end of the loop (after code). For-loops are sufficient for just about all loop-programming requirements. Here is a simple example of a loop:

    for (int x = 0; x < 5; x++) {
        System.out.println ("Hello world!  [x = " + x + "]");

This would print "Hello world! [x = Y]" five times, with `Y' being replaced the value of `x'.

Any or all of foo, bar, blip and code can be omitted, if they are not needed. For example, the loop:

    for (;;) {
        System.out.println ("Hello world!");

will print "Hello world!" forever (well, until the program is terminated). There is also a while-loop, which is a simplified for-loop.


An identifier is the name of something. This includes the names of variables, methods, classes, etc.

if statement

An `if' statement is used to conditionally execute code based on a boolean-expression. An `if' statement may optionally have an `else' part, that is executed if the boolean expression is false. For example:

    if (x == 42) {
        System.out.println ("x is 42");
    } else {
        System.out.println ("x is not 42");

In this case, the braces (`{' and `}') are not strictly required, however, it is good practice to always use them. The slight exception to this rule is when writing out cascading `if's, described below.

`if' statements can also be nested, to create a cascading effect. This is useful when testing for one `true' condition of many. For example:

    if (x == 42) {
        System.out.println ("x is 42");
    } else if (y == 42) {
        System.out.println ("x is not 42, but y is");
    } else if (z == 42) {
        System.out.println ("neither x nor y are 42, but z is");
    } else {
        System.out.println ("nothing was 42");

Strictly speaking, there could be more braces in this -- around each of the first two `else' conditions. However, since the `else' code is always another `if' (in the first two cases), these braces can be safely omitted -- it also helps to keep the indentation sane. Contrast with:

    if (x == 42) {
        System.out.println ("x is 42");
    } else {
        if (y == 42) {
            System.out.println ("x is not 42, but y is");
        } else {
            if (z == 42) {
                System.out.println ("neither x nor y are 42, but z is");
            } else {
                System.out.println ("nothing was 42");

That is significantly less pleaseant and a lot harder to read.


An `implementation', in Java-speak, refers to a class that provides an implementation of an interface.


Inheritance has two main uses in Java. Firstly, it allows for code re-use. When one class extends another, it inherits the public and protected methods and attributes from the class it is extending. The new class can override inherited methods, and/or add its own new methods and attributes. The second use of inheritance relates to the type-system. When one class extends another, the new class is type-compatible with the ancestor (and all ancestors back to `Object'). This type-compatability is similar to interfaces, but the `interface' is a real class.

Here is a fairly simple example demonstrating inheritance:

    public class Base
        public void sayHello ()
            System.out.println ("Hello base world!");

    public class Other extends Base
        public void sayHello ()
            System.out.println ("Hello other world!");

When the `Other' class extends `Base', it inherits the `sayHello()' method. This is then overridden to provide an `Other'-specific `sayHello()'.

To be completed


The term `instance' refers to a class that has been instantiated at run-time, also known as an object. Instances of classes are typically created using the `new' operator. For example:

    Foo x;
    x = new Foo ();

After this code has been executed, `x' is an instance of `Foo'. Unlike objects, it would be unusual (and incorrect) to refer to null-objects as instances. Furthermore, the type of an instance is not necessarily the same as used in its variable declaration. For example:

    Object x;

    x = new Foo ();

The `x' here is an `Object', that is an instance of `Foo'. Java provides a built-in `instanceof' operator that tests whether some object is an instance of some class. For example:

    public void display (Shape s)
        if (s instanceof Triangle) {
            Triangle t = (Triangle)s;            // type-cast


If the `Shape' given as a parameter is actually an instance of a `Triangle' then the code inside the if will be executed (performing the type-cast successfully).


To be completed


See int. Additionally, Java provides an Integer class, which encapsulates the int primitive-type.


An `interface' is a collection of method-signatures, that can be implemented by a class. Interfaces are defined in a slightly different way to classes, for example:

    public interface Enumerable
        public int getValue ();
        // other method signatures

The general idea is that we can then define classes that implement this interface -- and those classes must define a method called `getValue' with the same method-signature. For example:

    public class Foo implements Enumerable
        public int getValue ()
            return 42;

        // other methods/attributes belonging to "Foo"

Interfaces may also be used as data-types, but you cannot create a new interface. For example:

    public void thing (Enumerable e)
        System.out.println ("e\'s value is " + e.getValue ());

    public void test ()
        Foo f = new Foo ();

        thing (f);         // allowed because "Foo" implements "Enumerable"

loop counter

A `loop-counter' is the term given to the typically incrementing variable inside a for-loop. For example:

    for (int i = 0; i < 5; i++) {
        System.out.println ("Hello world!  [i = " + i + "]");

In this code, `i' is the loop-counter.


A method is a piece of code within class which performs some action upon that class. For example, the "Circle" class has a "setDiameter" method, which is used to set the diameter of the circle. Methods usually come in three flavours, accessors, mutators and `do-something' methods (a `goBoing' method for example).

method signature

A `method-signature' is a formal `description' of a method. This includes at least the method's name and return-type, and possibly formal-parameters, access-modifiers and whether the method is static. Method signatures are found on actual method declarations themselves, and inside interfaces. For example:

    public class Foo {
        private static int foo (int x)
            // body of foo

    public interface Bar {
        public double bar ();


A mutator is a method which is used to modify the value of something (usually a private variable inside the class). For example, the setWidth() method is a mutator. There is nothing special about mutators in Java terms, it's just a word used to describe what they do (modify something).


Briefly, names can be classified in the following ways, using the following styles:

  • File names. File-names should have the same name as the class contained within, plus the `.java' extension. For example, the source-code for a class called `FooBar' would be in a file called `'.
  • Class names. These start with uppercase letters and use capitalisation on words, e.g. `Person', `PetrolStation', `ServerSocket', `SomeOddlyNamedClassIMade'.
  • Method names. These start with lowercase letters and use capitalisation on words, e.g. `getHeight', `setAllOn', `doSomethingSilly'.
  • Variable names. Same as method names; these start with lowercase letters and use capitalisation on words, e.g. `myRobot', `x', `theWorld'.
  • Class constants. These are all uppercase with under-scores seperating words (as tends to be the naming style for constants in other languages), e.g. `MALE', `DAY_OF_WEEK', `DEBUG_BROKEN_THINGS'.

Much of the naming is convention, rather than enforced. It makes good sense, however, to pick one style and stick to it.

new operator

The new operator is used to create a new instance of a class. new takes the name of a class and arguments to any constructor as an argument, and returns a new instance of that class. Every class can be created with no arguments to its constructor, eg:

    x = new Foo ();
    tri = new Triangle ();
    area = new Board ();

These three assign the new object to a variable of the corresponding type. Often the declaration of an object will be accompanied by the creation of it, as in "Foo x = new Foo ();". Assignment need not necessarily be involved either, for example:

    Board area = new Board ();
    area.add (new Circle (100,, 0, 0));

I wouldn't exactly encourage using new in this way, as it makes the code harder to read/understand.


`null' is a special `nothing' value for objects. It is used (mainly) to distinguish between instantiated and non-instantiated object variables. `null' is a valid value for any object, but not primitive-types.


`object' refers (literally) to an instance of a class. ie, something generated by the new-operator.


Operators are used to combine operands (the operator's arguments) and produce a result. For primitive-types, a wide-range of operators are allowed, for example: `+' (add), `-' (subtract, unary-minus), `*' (multiply), `/' (divide), `%' (modulo), `<=' (less-than or equal), etc. For object types, only the `==' (equality) and `!=' (not-equal) operators are allowed -- and these test for the same-object, not whether the internal structure of those objects is the same.

The one slightly strange operator is the conditional-operator. This is a sort of mini `if-then-else' construct, but unlike an `if', it has a value. For example:

    y = ((x == 42) ? 0 : (x - 1));

The first part of the operator (before the `?') must be a boolean-expression. The remaining two expressions are the results. If the condition is `true', then the first expression (between `?' and `:') is the result. Otherwise the second expression (after the `:') is the result. This operator has many uses, but can result in slightly hard-to-read code.


to be completed..

primitive data type

Primative data-types are the building blocks of information storage. Java has a few of these:

  • boolean holds a 1-bit value (`true' or `false')
  • byte holds 8-bits of data (-128 to 127 inclusive)
  • short holds 16-bits of data (-32768 to 32767 inclusive)
  • char holds a unicode (16-bit) character (0 to 65535 inclusive)
  • int holds 32-bits of data (-2147483648 to 2147483647). This is the normal data-type used to store a whole number
  • long holds 64-bits of data (-9223372036854775808 to 9223372036854775807)
  • float holds a 32-bit floating-point number.
  • double holds a 64-bit floating-point number. This is the normal data-type used store non-integer (``not whole'') numbers


to be completed..


Repetition is what a loop performs, ie, repeat a certain code-block until a condition is met. This is handled by for and while loops in Java.

return type

The `return-type' of a method refers to what type of value it returns. For methods that don't return anything (i.e. return nothing), the return-type is `void'. Methods may return values of primitive-types, or objects.


The term `run-time' refers to things that occur when the program is executing (e.g. a user has `run' it).


Selection refers to the "switch" statement in Java. This allows the programmer to take a different actions based upon the value of an expression. For example:

    switch (x) {
    case 0:
    case 1:
        System.out.println ("foo " + x);
    case 2:
    case 3:
        System.out.println ("bar " + x);
        System.out.println ("something else " + x);

The expression inside the switch statement (in this case `x') must evaluate to a primitive-type. The values against the "case" statments must be constants of that type.


A statement is a piece of code which performs some action. Statements in programs are mostly assignments, method calls, conditionals, loops, and other control statements (return, throw, etc.). At the end of most statements, a semi-colon `;'is found. Statements are different from declarations, as a declaration does not perform any action.


The `static' keyword is a modifier that can be used on attribute and method declarations. Essentially it means ``there is only one of these, this one''. As there is only one, `static' attributes and methods are not associated with instances of classes, but the class itself. For example:

    class Foo {
        public static int x;
        public int y;

        public static void bar ()
            // body of bar

The ordinary attribute `int y' belongs to instances of `Foo' -- i.e. it does not exist without an instance of `Foo'. The static attribute `static int x', however, belongs to the class, so it always exists, as long as `Foo' is defined.

`static' attributes and methods are referenced using the class name -- `Foo' in this case. For example:

    int z = Foo.x; ();
    Foo.x = 42;

Inside `Foo's methods, the `Foo.' prefix is often un-necssary. The `static' attributes and methods of a class are visible to its instances as well, that means they can be accessed in conjunction with an instance, as well as `stand-alone'. For example, the following method could be added to the `Foo' class:

    public int update ()
        y = x;
        x = 0;

Because this is a non-static method, we require an instance of `Foo' to call it. For example:

    Foo a, b;

    a = new Foo ();
    b = new Foo ();
    a.x = 99;
    b.update ();

    System.out.println ("b.y = " + b.y + ", Foo.x = " + Foo.x);

When run, this code fragment will output ``b.y = 99, Foo.x = 0'', because the `x' is effectively shared between the two instances of `Foo'.


A string is a sequence of characters. In Java, strings are encapsulated by the String class. String constants are defined with ` "this is a string" ' (some text inside double-quotes). Java has some special handling for strings, which although being inconsistent makes the programmer's job much easier. Notably there is `+' operator on strings which performs concatenation, for example, "System.out.println ("The value of x is " + x + ". see ?");". Another oddity is that here the integer `x' is automatically converted to a string. Similar behavior from objects can be obtained by providing a class with a toString() method.


Termination of a Java program happens when:

  • the user presses Ctrl-C while the program is running
  • the the flow-of-control reaches the end of the main method
  • the program executes "System.exit (0);"


A type-cast is used to convert between data-types in Java. For primitive-types, type-casts extend or truncate precision, or change the representation of, a number. Type-casts are specified as they are in C -- with the `target' type in parenthesis before the `source' (thing being cast). For example:

    int x;
    double d;

    d = 42.5;
    x = (int)d;

One thing to be wary of: what is the value of `x' after the type-cast ? It's 42, rather than 43. This behaviour (of integer to floating-point conversion) is typically known as ``round-to-zero''. Some languages allow you to specify how such conversions should be handled, other languages (such as occam) require the conversion to be specified exactly (rounded or truncated). You can fake ``round-to-nearest'' behaviour by adding 0.5 before the type-cast (conversion).

Type-casts of object types are somewhat different, but are performed using the same syntax. An object can only be cast to something that it already is, i.e. a class that is present in the object's inheritance heirarchy.

java type-cast shape Returning to the shape/hole analogy given in data-types, an object type-cast is a sort of `adapter', as shown in the picture to the right. In this case, the adapter is a type-cast from `Object' to `Integer'. Whatever we plug into the `Object' hole must really be an `Integer'. If it is not, a `ClassCastException' exception is thrown. For example:

    public void foo (Object obj)
        Integer i;

        i = (Integer)obj;         // type-cast

    public void bar ()
        Integer i = new Integer (42);

        foo (i);

    public void blip ()
        String s = "hello, world!";

        foo (s);

If the `bar' method is called, a new `Integer' is created and passed as an `Object' to `foo'. `foo' then casts this back into an `Integer', that will succeed, since the `Object' it sees really is an `Integer'. On the other hand, if `blip' is called, is passes a new `String' (as an `Object') to `foo'. When `foo' attempts to cast this into an `Integer', it will fail, throwing `ClassCastException'.


A variable in Java is basically something in which information can be stored (appear on the left-hand side of an assignment statement). Variables are referred to by name in the code, for example "int x;" declares an integer variable called `x'.

while loop

A while-loop has the syntax "while (condition) { code }". As long as the boolean-expression `condition' evaluates to true, the statements comprising `code' will be executed. This is equivalent to the for-loop "for (; condition;) { code }". Java also has a do-while loop, which is similar, but the condition is checked after the code has been executed: "do { code } while (condition);". This tends to be less common however, as `code' is executed at least once, before `condition' is checked.

Last modified: 2006-10-27 01:55:27.000000000 +0100 by Fred Barnes [ls] [plain]
Page generated: Sun Apr 28 11:39:33 2013
Valid XHTML 1.0! Valid CSS!