Sunday, March 19, 2006



The Java programming language is related to C and C++, but includes ideas from several other languages (Gosling et al. 2000).




The computer language C is a descendant from B (Kernighan & Ritchie 1988:1).


  • Kernighan, Brian W. & Dennis M. Ritchie. (1988). The C programming language. New Jersey: Prentice Hall.



The computer language B is a directly descendant from BCPL (Thompson 1972:1).


Author Thompson, Ken
Year 1972
Title Users' Reference to B
Publisher Bell Laboratories
Retrieved March 19, 2005

Saturday, March 18, 2006



If you want your program to do anything at all, you will have to write statements. Just like the sentence is the smallest structure that make any sense to humans, a statement is the smalest structure that make any sense to the computer. Statements consists of tokens and expressions, like sentences consist of words and phrases in English.

A statement is similar to a sentence in a natural language like English, except that a statement tell the computer how to do something.

Concept: A statement is the smallest part of a program that the computer can understand.


class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!");


In the application above, System.out.println("Hello World!"); is a statement, and the only line which actually does something.

A statement in Java must be terminated with the semicolon, the ';' character.


The following is the Java grammar for a Java statement:

Statement =
StatementWithoutTrailingSubstatement | LabeledStatement | IfThenStatement | IfThenElseStatement | WhileStatement | ForStatement ;

The grammar look more difficult than it really is. Just click around in the grammar to get a feeling what you may do with a statement.

Wednesday, March 15, 2006

The Semicolon


The semicolon, ;, was invented by Aldus Manutius the elder (1450-1515), an Italian printer. Manutius used the semicolon to separate words opposed in meaning and to mark off mutually dependent statements (

The semicolon in Java works much the same way as the punctuation mark in English. The punctuation mark terminates a sentence in English, so that you may write several sentences following each other. The semicolon in Java terminates a statement, so that you may write several statements following each other in sequence.


So, why does most programming languages use the semicolon instead of the punctuation mark? The answer is that the semicolon is supposed to do several things. The semicolon terminates statements, separate statements and concatenates statements. Besides, the punctuation mark is used for other purposes.

The Order of Execution

Statements are executed one at a time, from top to bottom, left to right. The computer gets one statement and executes it, and then gets the next statement followed by the semicolon.

Key Concept: A sequence is ordered statements that are executed in the same order as written. Statements are sequenced (ordered) by the semicolon.

Key Concept: Any statement which does not perform an explicit jump, actually performs an implicit jump to the next statement because of the semicolon.

Note: The semicolon is not an operator because operators are used to combine factors in an expression. That is, operators are not used to combine statements.

Consider the code:
class StarTrek {
    public static void main(String[] args) {
        System.out.println("              Space, the final frontier.");
        System.out.println("   These are the voyages of the Starship Enterprise.");
        System.out.println("Its continuing mission: to explore strange new worlds;");
        System.out.println("      to seek out new life and new civilizations;");
        System.out.println("      to boldly go where no one has gone before.");
[user]$ javac
[user]$ java StarTrek
              Space, the final frontier.

   These are the voyages of the Starship Enterprise.
Its continuing mission: to explore strange new worlds;
      to seek out new life and new civilizations;
      to boldly go where no one has gone before.

Obviously, the order of execution is normally of importance for the result. The Star Trek mission statement would not give any meaning stated in a different order.


Semicolons may make a program look cluttered, but with semicolons, long statements can run over several lines with new line almost anywhere you prefer. With semicolon, several statements can also be placed on the same line, but this should generally be avoided. It was usually done to save print paper on old computers without a monitor. A statement should be written beneath the statement above, because of readability.

How to terminate statements


In Java, each statement must be followed by the ; character. Even the last statement must be followed by the semicolon, so that you do not forget to add the semicolon if you later have to move the statement. It would also be confusing if some statements need semicolon and some statements don't. In the programming language Pascal, you don't need semicolon after the last statement. Now, what happens if you later have to move the last statement and you sort of forget adding the semicolon? This is a constant source of compilation errors in Pascal. The solution is to add an empty statement, the semicolon, after the last statement, which by "coincidence" is what you have to do in Java anyway.

Each individual statement is terminated by a semicolon. (The C programming language, Second Edition, 1988, Brian W. Kernighan, Dennis M. Ritchie. Prentice Hall, s. 10.)

The semicolon is a statement terminator in Java, rather than a separator. Java got this from C: "In C, the semicolon is a statement terminator, rather than a separator as it is in languages like Pascal." (The C programming language, Second Edition, 1988, Brian W. Kernighan, Dennis M. Ritchie. Prentice Hall, s. 55.).

In Java, each statement must be terminated with the semicolon. Even the last statement must be terminated with semicolon, because it is easy to forget to add the semicolon if you later have to move some statements. Also, it would be confusing if some statements need semicolon and some statements don't.

The goon Symbol

The semicolon means that after this statement has been executed and terminated, then go on to the next statement following this statement. (Lykkenborg 2005)

C got the semicolon from ALGOL 68. The ALGOL 68 semicolon is called the GOON SYMBOL pronounced "go on" (

The 'go on symbols' (better known as semicolons) constitute the mortar which bind the bricks together (Lindsey & Meulen 1974).

  3.0.7. Sequencing tokens
  go on symbol        ;
  completion symbol   .
  go to symbol        go to
(Wijngaarden et al. 1968).

The Empty Statement

The empty statement denotes the null action (Wirth 1983).

The Sequence

"The most basic structure is the sequence. A computation is a sequence of actions, where each action is specified by a statement, and is executed after the preceding action is completed. This strict sequentiality in time is an essential assumption of sequential programming. If a statement S1 follows S0, then we indicate this sequentiality by a semicolon S0; S1" (Wirth 1983:17).

The Statement Separator

"This statement separator (not terminator) indicates that the action specified by S0 is to be followed immediately by the action corresponding to S1." (Wirth 1983:17).


Author Wirth, Niklaus
Year 1983
Title Programming in Modula-2, Second Edition
Place Berlin Heidelberg New York
Publisher Springer-Verlag

Author Wijngaarden, A. van (editor), Mailloux, B. J., Peck, J.E.L. & Koster, C.H.A.
Year 1968
Title Draft report on the algorithmic language ALGOL 68, third printing
Place Amsterdam
Publisher Mathematisch centrum

Author Lindsey, C.H., Meulen, SG. van der
Year 1974
Title Informal Introduction to Algol 68

Wednesday, March 08, 2006

Binomial Coefficient


Binomial Coefficient, nCr = n! / ((n - r)! * r!).


This code was derived from algorithms after Manolopoulos (2002) and User Contributed Notes (2006).

package math;

public final class Main {

     * Don't let anyone instantiate this class.
    private Main() {
    public static long binomialCoefficient(int n, int r) {
        long t = 1;
        int m = n - r; // r = Math.max(r, n - r);
        if (r < m) {
            r = m;
        for (int i = n, j = 1; i > r; i--, j++) {
            t = t * i / j;
        return t;
    public static void main(String[] args) {
        // possible combinations of cards in a poker hand
        System.out.println(binomialCoefficient(52, 5)); // 2598960
        // possible combinations of numbers in lottery
        System.out.println(binomialCoefficient(49, 6)); // 13983816


Author Manolopoulos, Yannis
Year 2002
Title Binomial Coefficient Computation: Recursion or Iteration?
Publisher ACM SIGCSE Bulletin, Volume 34, Issue 4

Author User Contributed Notes
Year 2006
Title Mastering Algorithms with Perl (Owant). In: LXXII. Mathematical Functions
Publisher php
Retrieved March 8, 2006 from