|
About This Short CourseBy
February 2001
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
class Vehicle {
...
}
class Car extends Vehicle {
...
}
class Truck extends Vehicle {
...
}
|
Program execution begins in the main method of any of the classes in your program;
you specify which class when you launch the program.
Variables in COBOL are either elementary or group items
and correspond loosely to primitive variables (such
as integers, characters, and real numbers) and objects in the Java programming
language. COBOL is not strongly typed like the Java programming language,
however. In COBOL, you define a picture; of what
can be stored in the variable, whereas the Java programming language requires a
rigid type to be associated with that data storage element. A "PIC" can be alphabetic (A), alphanumeric (X), or
numeric (9). The commonly used Java programming language primitives are boolean, char, String, int, and float.
COBOL uses level numbers on all data items:
You initialize items with a VALUE clause such as:
01 Result PIC 99 VALUE ZEROS.
In the Java programming language, there are no level numbers. You must explicitly label something as a primitive or a class (similar to a group) definition. You initialize variables with an assignment operator:
int result = 0;
Here is a simple group called TheDate:
DATA DIVISION.
WORKING-STORAGE SECTION.
01 TheDate.
02 CurrentYear PIC 9(4).
02 CurrentMonth PIC 99.
02 CurrentDay PIC 99.
|
You can approximate this group of three variables in a Java program with a class definition:
class TheDate {
String currentYear;
String currentMonth;
String currentDay;
}
The differences are primarily that the COBOL group
defines actual storage space and the exact memory layout (picture), whereas the Java class definition describes a
template for storage rather than the storage itself. A running Java program is a
collection of objects. Objects with the same characteristics and behavior are
described by the same class. Objects of the same type (class) are therefore
considered instances of that class. To allocate
actual storage for an object, use the new
operator:
// make d refer to a TheDate type object. TheDate d = new TheDate();
A better way to look at the difference between a class
and an object is, perhaps, the difference between a FILE and a RECORD. A
record is a collection of fields and a file is a collection of records. There is
only one record definition, but there may be many records within a file with
that same structure. Similarly, for any class definition, there may be many
class instances, objects. The type (class) defines the structure of the object
and the object stores the content.
On the other hand, COBOL reads files one record at a
time--there is exactly one record from a file in memory at once (the record buffer). In a Java runtime environment, the
instances of a class all exist in memory at once. If you want 3000 TheDate objects, you have to have enough memory to hold
3000 of these objects.
The following file descriptor entry describes a student
file with a record structure specified in the StudentRecord group.
DATA DIVISION.
FILE SECTION.
FD StudentFile.
01 StudentRecord.
02 StudentId PIC 9(7).
02 StudentName.
03 Surname PIC X(8).
03 Initials PIC XX.
|
You can imagine that the records in the file are like "freeze-dried" Java objects; that is, they are on the disk instead of in memory.
Sometimes you have a COBOL file containing records of different record types. Because you cannot always establish the record type just by examining its contents, you normally put a special data item at the start of each record indicating the record or transaction type.
FD TransactionLogFile. 01 StudentRecord. 02 RecordType PIC X. ...
The Java runtime environment has a similar notion for objects. You can ask every object for its type. The type information refers to the object's class definition, which includes the list of methods, data fields, and so on.
COBOL has tables that hold contiguous sequences of elements in memory just like a file holds contiguous sequences of records. Each element is uniquely identified by an integer index (subscript in COBOL terminology) within the table. Instead of having 5 name variables called name1, name2, ..., name5 you would define a table with 5 elements:
02 NAMES OCCURS 5 TIMES PIC X(15).
The third name variable would correspond to the third element in the table. You access the elements of a table by specifying the table name followed by the index in parentheses:
MOVE 'John' TO names(3).
Table elements are typically accessed in PERFORM "loop" statements:
PERFORM VARYING Idx FROM 1 BY 1
UNTIL Idx GREATER THAN 5
DISPLAY names(Idx)
END-PERFORM
The Java programming language has arrays that correspond
to COBOL tables, however, Java arrays are themselves objects and are indexed
from 0 instead of 1. You must use the new operator
to create space for an array object. Here is the Java source code that declares
an array called names containing five strings:
// elements names[0]..names[4] String[] names = new String[5];
To set array elements, use the name followed by square brackets instead of parentheses:
names[3] = "John";
Loops in the Java programming language are also often
used to access arrays. The following loop prints out the elements within the names array.
for (int i=0; i<=4; i=i+1) { // i="0..4"
System.out.println(names[i]);
}
thisThe notion of the this
variable in the Java programming language, referring to the target of a message
send, has a few analogs in the COBOL world.
In COBOL, the record buffer associated with a file is
sort of a cursor or current
record buffer that changes as you read through the file. If you walk
through a list of Java objects, sending them each a message, the this variable will refer to each of the objects in turn
just like the moving file cursor.
The ambiguity resolution or scope
override ability of the this variable also has
an analog in COBOL. When you have two items in two different tables or files
with the same name, you must use the OF clause to
indicate which one you are referring to such as StudentName OF StudentFile.
Beyond the differences in the data side of COBOL and Java programs, there are differences when executing them, too.
In COBOL, execution begins at the first procedure in the
PROCEDURE DIVISION and terminates with the
statement:
STOP RUN.
In a Java runtime environment, program execution begins
in the main method of the class you tell the Java
runtime to start with. For example, here is a class with a main method that prints "Hello":
public class Simple {
public static void main(String[] args) {
System.out.println("Hello");
}
}
From the command line, you would execute this program by saying:
java Simple
In strongly typed languages like the Java programming
language, assignments are only allowed between compatible types. Assignment in
Java programs corresponds to the MOVE statement in
COBOL; in other words, MOVE is really a
misnomer--COPY would have been more accurate.
Assignment copies from source to destination(s).
You can assign a variable to another variable or a literal to a variable as in :
MOVE AVERAGE-VALUE TO SUM. MOVE 'John' to NAME.
With the Java programming language, you would say:
sum = averageValue; name = "John";
The assignment statement in the Java program is very simple compared to COBOL; there is no truncation or filling of space ala COBOL. The types of the left and right hand sides must be compatible or they are not allowed by the compiler.
To display results to the terminal in COBOL, you use the
DISPLAY command:
DISPLAY "AVERAGE: ", AVERAGE.
The Java programming language has a similar statement:
System.out.println("AVERAGE: " + average);
To display more than just a string, you build up a comma-separated list of elements in COBOL:
DISPLAY "AVERAGE: ", AVERAGE WITH NO ADVANCING.
In the Java programming language, you build up a string, which is then displayed. The plus operator in the Java programming language concatenates two strings:
System.out.print("AVERAGE: " + average);
Where COBOL uses the DISPLAY WITH NO ADVANCING statement
to cause the output not to advance to the next line, with the Java programming
language you use the System.out.print
command.
COBOL has two mechanisms that correspond to method calls
in Java programs: PERFORMing paragraphs and calling
subprograms.
COBOL procedures are like Java methods with neither
return values nor arguments. In the following fragment, execution flows from Begin to procedure Blort
to display Hello.
PROCEDURE DIVISION.
Begin.
PERFORM Blort
STOP RUN.
Blort.
DISPLAY "Hello".
|
In Java programs, you would have the following:
class Whatever {
void begin() {
blort();
}
void blort() {
System.out.println("Hello");
}
}
|
In the Java programming language, you can think of {...} statement groups as COBOL unnamed paragraphs.
For-loops in the Java programming language are like PERFORM in-lines.
You cannot pass parameters to a procedure. You need to use subprograms in COBOL for that. In the Java programming language, you can pass parameters to a method or not depending on your needs.
A method call corresponds to CALL of program in COBOL. You can have a single return
value in both the Java programming language and COBOL, but you can pass in items
that the subprogram modifies. COBOL subprogram calls pass parameters with the
USING clause and the return value is found in RETURN-CODE:
CALL 'DISPLAY-COUNT' USING COUNT.
* use RETURN-CODE if you want
IF RETURN-CODE
...
END-IF
With the Java programming language, you use the following syntax:
displayCount(count);
Primitive types in the Java programming language such as
int can only be passed by value (CONTENT in COBOL) and all objects
are passed by REFERENCE.
The definition of a program with parameters in COBOL
requires that you define the storage in the LINKAGE
SECTION and then identify them on the PROCEDURE
DIVISION header:
IDENTIFICATION DIVISION.
PROGRAM-ID DISPLAY-COUNT IS INITIAL.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 AVERAGE PIC 9(8) VALUE 0.
LINKAGE SECTION.
01 COUNT PIC 9(8).
PROCEDURE DIVISION USING COUNT.
Begin.
DISPLAY "COUNT: ", COUNT
EXIT PROGRAM.
|
The variables you define in the WORKING-STORAGE SECTION are initialized each time you
call the subprogram if you use the IS INITIAL on
the PROGRAM-ID statement. This corresponds closely
with the Java programming language. Here is the equivalent Java method to the
above DisplayCount program:
void displayCount(int count) {
// set average to zero upon each entry
int average = 0;
System.out.println("COUNT: " + count);
}
Please see the quick reference at the end of this document for information on other executable statements.
In COBOL, contained subprograms are not visible to other
contained subprograms by default. You must use the IS
COMMON PROGRAM clause to make a subprogram visible to other subprograms:
IDENTIFICATION DIVISION.
PROGRAM-ID. MAIN.
PROCEDURE DIVISION.
...
EXIT PROGRAM.
IDENTIFICATION DIVISION.
PROGRAM-ID. A.
* CAN CALL B
CALL B.
END-PROGRAM A.
IDENTIFICATION DIVISION.
PROGRAM-ID. B IS COMMON PROGRAM.
* CANNOT CALL A
CALL A
END-PROGRAM B.
END-PROGRAM MAIN.
|
The Java programming language has data hiding, but it is
used to restrict access to methods within a class from methods outside of the
class. Class members labeled as public are visible
to any method in any other class. Class members labeled as private are not
visible to any method in any other class.
| Statements | |||
|---|---|---|---|
| COBOL | Java | ||
MOVE b TO a |
a=b; |
||
MOVE a TO arrayName(index) |
arrayName[index]=a; |
||
ADD b TO a |
a = a+b; or a+=b; |
||
ADD b TO a GIVING c |
c = a+b; |
||
ADD 1 TO a |
a++; or a+=1; or a=a+1; |
||
IF conditional statements1 ELSE statements2 END-IF |
if ( conditional ) {
statements1
}
else {
statements2
}
|
||
|
|
||
PERFORM TEST BEFORE UNTIL conditional statements END-PERFORM |
while (conditional) {
statements
}
|
||
PERFORM TEST AFTER UNTIL conditional statements END-PERFORM |
do {
statements
} while (conditional);
|
||
PERFORM VARYING a FROM start BY inc UNTIL conditional statements END-PERFORM |
for (a=start;
conditional;
a+=inc) {
statements
}
|
||
PERFORM routine.
|
routine();
|
||
CALL 'DUMMY-FUNCTION' USING arguments. |
dummyFunction(arguments); |
||
| Relational Operators | |
|---|---|
| COBOL | Java |
a EQUAL TO b |
a==b for
primitive typesa.equals(b) for objects
|
a NOT EQUAL b |
a!=b for
primitive types!(a.equals(b)) for objects
|
a GREATER THAN b |
a>b
for primitive types
|
a GREATER THAN OR EQUAL b |
a>=b
for primitive types
|
a LESS THAN b |
a<b
for primitive types
|
a LESS THAN OR EQUAL b |
a<=b
for primitive types
|
| Boolean Operators | |
|---|---|
| COBOL | Java |
a AND b |
a&&b for boolean expressions a, b
|
a OR b |
a||b for
boolean expressions a, b
|
Copyright 1996-2001 jGuru.com. All Rights Reserved.
|
| ||||||||||||