Skip to main content
IBM 
ShopSupportDownloads
IBM HomeProductsConsultingIndustriesNewsAbout IBM
Java Language EssentialsDownload tutorial zip fileEmail this tutorial to a friend
Main menuSection menuGive feedback on this tutorialPrevious panelNext panel
Course Notes
  


Executable statements page 35 of 37


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.

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 programing 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);

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 Quick reference at the end of this document for information on other executable statements.


copyright 1996-2000 Magelang Institute dba jGuru


Main menuSection menuGive feedback on this tutorialPrevious panelNext panel
PrivacyLegalContact