Java vs Pascal

PGSS Computer Science Core

Many of you have already studied Pascal and just need to learn the syntax of Java. Pascal and Java, only distantly related, have many differences. This document attempts to summarize the differences.

Unclassified

General

In general, Java is a much more succinct language. This is putting it nicely. To put it meanly, its syntax is brutal. For historical reasons the syntax comes from C, which hase even more brutal syntax. This is something with which we must bear.

The first brutal syntax note of Java is that case of characters in Java is significant, so the variable names ``i'' and ``I'' are distinct. You should naturally not name variables where only case matters.

Another significant difference between Pascal and Java is that in Pascal the semicolon separates statements while in Java the semicolon terminates statements. So, while in Pascal one may omit the semicolon for the statement just preceding an ``end'', a semicolon is required in Java for statements preceding its equivalent, ``}''.

Data types

The basic Java type we are using are char, int, and double. These correspond to Pascal's char, integer, and real.

Pascal and Java arrays are very different. In Java, the array type is written char[]. The bounds of the arrays implicitly range from zero up to one less than the length of the array. You may not define the bounds of the array yourself.

Comments

Pascal comments are begun by `{' or `(*' and ended by `}' or `*)'. In Java, comments begin with a double slash `//' and continue to the end of the line. To span multiple lines, each line should have a double slash.

(Java also allows comments to begin with `/*' and to end with `*/'. We have been ignoring these in this course.)

Subprograms

Functions

In Pascal, one would write a function like this.

  function FuncName(parameters) : returnType;
  var
    variableDeclarations;
  begin
    functionBody;
  end;
The analogous thing in Java is the following.
  public static returnType FuncName(parameters) {
    variableDeclarations;

    functionBody;
  }
For the purposes of this class, the ``public static'' keywords are required.

Unlike Pascal, functions cannot be nested. This simplifies the visibility of variables considerably: the only variables visible are those declared within the function and those declared so that the entire program can see them, and we're ignoring the latter in this class.

Function calls are just as in Pascal.

Return values

In Java, you specify the return value of a function using an explicit return statement.

  return expression;
This immediately exits the function with a return value equal to the value of the expression.

Procedures

Java does not have the procedure construct of Java. Rather than a procedure, use a function whose return value is of type ``void''.

  public static void ProcedureName(parameters) {
  }

You can exit this function either with a return statement with no expression (``return;'') or by reaching the closing brace of the function.

You can call this as in Pascal.

Parameters

The parameter list in Java is also considerably different from Pascal. Parameters are a comma-separated list of type and parameter name. For example,

  public static returnType FuncName(int parm1, double parm2, int parm3) {
    // ...
  }
You may not combine types as in Pascal. That is, if you have two integer parameters adjacent to each other, you must type ``int parm1, int parm2''.

Pascal allows the possibility of call-by-reference, using the function declaration

  function funcName(var a: integer) : real;
All parameters of the basic Java types are call-by-value. There is no way to use call-by-reference.

On the other hand, all arrays are in some sense call-by-reference. That is, if you change an element of an array, the value will be changed in the calling function.

Variables and expressions

Variables

To declare a variable in Java, type the type, followed by the variable name, followed by a semicolon:

  variableType variableName;
Or, more concretely, to declare an integer i, you would use
  int i;

Variable names in Java may have any number of letters, digits, and underscores (``_''). The first character, however, must be a letter. Notice once again that the case of the letters is significant.

Arrays

Arrays are slightly more difficult but not outrageous. An array declaration would be

  arrayEntryType variableName = new arrayEntryType[arrayLength];
An array of thirty characters named arr would be declared
  char[] arr = new char[30];
Note once again that arrays are indexed from 0 to one less than the array length. This means that arr[0] exists while arr[30] does not! (You cannot, as in Pascal, change the array bounds to start elsewhere.)

Multi-dimensional arrays are naturally possible. The declaration would be

  arrayEntryType[][] variableName = new arrayEntryType[numberOfRows][rowLength];
A 4-by-5 two-dimensional matrix of reals named mat, then, would be declared
  double[][] mat = new double[4][5];

To access an element of the array, you use brackets as in Pascal. arr[20], then, is the 21st character in arr. For multi-dimensional arrays, however, You use separate brackets for each index rather than a comma. So the first element of mat is mat[0][0].

Expressions

Java allows many of the same arithmetic operators as in Pascal. The `*', `/', `+', and `-' remain the same, except that `/' is the same as `div' when both sides are ints. (Always think twice when dividing to insure that you are going to get what you want.)

The `mod' operator is written as `%' in Java.

Accessing multi-dimensional arrays is different from Pascal. See the section on arrays for details.

Conditions

Several of the conditional operators of Java are different from those of Java. Here is a decoding.

  Pascal   Java
    =       ==
    <>      !=
    >       >
    <       <
    >=      >=
    <=      <=
    in     not available
    and     &&
    or      ||
    not     !
Java has an order of precedence for the `!', `||', and `&&' operators. It is a good idea to always parenthesize when combining them.

Statements

Assignment statements

Assignment in Java is done using `=', not `:='. The statement is always terminated by a semicolon.

if statements

The if statement is a bit different. The following block in Pascal

  if condition then
    begin
       doThis
    end;
becomes in Java
  if(condition) {
    doThis;
  }
An else is also available. Here is how to do it.
  if(condition) {
    doThis;
  } else {
    doThat;
  }

while loops

The Pascal while loop

  while condition do
    begin
      doThis
    end;
becomes in Java
  while(condition) {
    doThis;
  }

Note that Pascal does not allow you to exit a loop within the loop (save with a goto statement). Java gives a break statement to do this; see below.

for loops

The Pascal for loop

  for var := lowerBound to upperBound do
    begin
      doThis
    end;
becomes in Java
  for(var = lowerBound; var <= upperBound; var = var + 1) {
    doThis;
  }
Many times, however, the for loop often does not translate directly, due to the fact that Java arrays are indexed from zero. To step through an array, you would write
  for(var = 0; var < arr.length; var = var + 1) {
    doThis;
  }

Note that Pascal does not allow you to exit a loop within the loop (save with a goto statement). Java gives a break statement to do this; see below.

Breaking a loop

If you want to exit a loop in its middle, Java allows you to do this via the break statement.

  for(i = 2; i * i <= n; i = i + 1) {
	if(n % i == 0) {
	  break;
	}
  }
In this example, the break will exit the for loop when reached, without trying any instructions that may follow.

Printing

Java has a different function to display values to the screen. This is called System.out.println(), and there is a separate page describing this.