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. It is not a complete replacement to what you see in the textbook, but it does point out where the differences lie.
In general, Java is a much more succinct language. This is putting it nicely; bluntly, Java's syntax is brutal. Historically this derives from C, which is even more brutal.
The first difference between Java and Pascal 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 ``else'' or ``end'', a semicolon is required in Java for statements preceding the equivalent of ``end,'' the closing brace ``}''.
The basic Java type we use are char, int, double, and boolean. These correspond to Pascal's char, integer, real, and boolean.
Pascal and Java arrays are very different. In Java, the array type is written char[]. The bounds of the arrays always range from zero up to one less than the length of the array. You may not define the bounds of the array to begin at something other than 0.
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. Comments spanning multiple lines should begin each line with a double slash. (Java also allows comments to begin with `/*' and to end with `*/'. We have been ignoring these in this course.)
A Pascal program looks roughly like the following.
program A; functionDeclarations begin mainBody end.The equivalent Java program would be
public class A { functionDeclarations public static void main(String[] args) { mainBody; } }That is, we enclose the entire program with ``public class A { ... }'', where A is the name of the program. The main body that is executed is declared with the special name of main.
The file containing the Java program must be named ``A.java'' - with the same name A given the class inside the file.
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.
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.
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.
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.
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 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].
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.
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.
Assignment in Java is done using `=', not `:='. The statement is always terminated by a semicolon.
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; }
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.
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.
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.
Java has a different function to display values to the screen. In this class we use a library called ``Console.java'', described in Appendix B of of the textbook.
href=printing.html>a separate page describing this.