Sun Java Solaris Communities My SDN Account Join SDN
 
Tutorials & Code Camps

jGuru: Language Essentials: Introduction

 

About This Short Course

By jGuru

February 2001

[Short Course | Exercises]

Appendix: Assistance for COBOL programmers

Overview

This appendix is meant as a learning aid to show the relationships between COBOL and some syntax and object-oriented semantics of Java technology for those COBOL programmers without C, Pascal, or C++ experience. This assumes that you have been through or are going through an object-oriented programming tutorial or course.

For your convenience, a quick reference guide is provided at the end.

Program Organization

A COBOL program is divided up into four divisions:

  1. IDENTIFICATION DIVISION.
    Defines the name of the program
  2. ENVIRONMENT DIVISION.
    Defines computer-specific environment details
  3. DATA DIVISION.
    Defines variables, input/output formats, constants, and work areas (storage space)
  4. PROCEDURE DIVISION.
    A group of procedures (paragraphs) that do the work of the program

COBOL programs explicitly separate the data and procedures that operate on the data. Procedures are called methods in the Java programming language.

Most COBOL statements are divided up into "area A" (four characters wide starting from left edge) and "area B" (the rest of the line). All this means is that the division, section, paragraph, and 01 level data start in A and the executable statements start in B. The Java programming language compiler has no such formatting restrictions that impart meaning and ignores white space for the most part. Also, the Java programming language is case-sensitive so that dog and Dog are totally different names.

Java programs are organized by units that correspond to entities in the real world: objects that encapsulate both the data and the methods to manipulate that data. Objects with the same characteristics and behavior are described by classes (storage templates) and correspond roughly to COBOL groups. A Java program is then primarily just a collection of class definitions such as:

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.

Variable Declarations

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:

  • 01 is reserved for group names, which begin in area A
  • elementary items begin in area B and use user-defined level values

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;

Data Aggregates (records/objects)

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.

Arrays

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]);
}

Scope Overrides and this

The 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.

Executable Statements

Beyond the differences in the data side of COBOL and Java programs, there are differences when executing them, too.

Program Execution/Termination

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

Assignment

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.

Displaying Results

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.

Calling Subprograms/Methods

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.

Symbol Visibility

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.

Quick Reference

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
}
EVALUATE conditional 
WHEN value1
  statements1 
WHEN value2
  statements2 
WHEN OTHER
  statements3 
END-EVALUATE

switch (conditional) {
  case value1:
    statements1
    break;
  case value2:
    statements2
    break;
  default:
    statements3
}

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 types
a.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.