PreviousCalling Object COBOL from Java Client/Server BindingNext

Chapter 5: Java Data Types

Java data types are different from COBOL data types. This chapter describes how COBOL and Java data types are mapped on to each other.

5.1 Overview

The Java language defines its own data types, which are different from the ones used in COBOL. The COBOL run-time system automatically converts between COBOL and Java types whenever you call Java from COBOL or COBOL from Java, as shown in Figure 5-1.

Mapping data between COBOL and Java

Figure 5-1: Conversion of Data Types

Numeric data types are converted in the same way whether the COBOL program is a procedural program, or Object COBOL. Objects and strings are converted differently though, depending on whether you are using procedural COBOL or Object COBOL. Object COBOL handles Java objects as COBOL object references, but procedural COBOL handles them as pointers.

5.2 Java Data Type Conversion Rules

When you send COBOL data to Java, it is converted to an appropriate Java data type. Similarly, when a Java program sends Java data back to COBOL, it is converted to a COBOL data type. The table below defines the conversions which happen when data is passed between Java and COBOL. Some data types are handled differently depending on whether you are using procedural COBOL (see the chapter Calling Procedural COBOL from Java) or the Object COBOL Java domain (see the chapters Calling Java from COBOL and Calling Object COBOL from Java). The COBOL column in the table shows you the conversion rules for procedural COBOL, and the Object COBOL column shows you the rules for the Object COBOL Java domain.

For convenience, copyfile javatypes.cpy defines a set of COBOL data types corresponding to Java data types - you can use these as a shorthand way of declaring data items for use with Java in your COBOL programs. These are shown in the User-defined Java Data Typecolumn. You can find javatypes.cpy in your $COBDIR/cpylib directory. We recommend that you set up your COBCPY environment variable to include this directory.

Java Data Type User-defined Java Data Type COBOL Data Type Object COBOL Data Type Description
byte jbyte PIC S99 COMP-5 PIC S99 COMP-5 Signed 1-byte integer
short jshort PIC S9(4) COMP-5 PIC S9(4) COMP-5 Signed 2-byte integer
int jint PIC S9(9) COMP-5 PIC S9(9) COMP-5 Signed 4-byte integer
long jlong PIC S9(18) COMP-5 PIC S9(18) COMP-5 (BY REFERENCE only) Signed 8-byte integer
boolean jboolean PIC 99 COMP-5 PIC 99 COMP-5 Zero value is false, non-zero is true
char jchar (Unicode) PIC 9(4) COMP-5 (Unicode) PIC 9(4) COMP-5 All characters in Java are represented by 2-byte Unicode characters
float jfloat COMP-1 COMP-1 (BY REFERENCE only) Floating-point number
double jdouble COMP-2 COMP-2 (BY REFERENCE only) Double-precision floating-point number
String mf-jstring POINTER PIC X(n) mf-jstring is a user-defined type giving the address, size and capacity of a string or buffer. For a String, the capacity is always zero. You should consider a string passed into a COBOL program as read-only, and not to be amended. For a StringBuffer, the capacity is the total size of the buffer, and the size is the length of the string currently held in the buffer.
StringBuffer
object   POINTER Object reference Any Java object. The pointer returned to procedural COBOL can be used with JNI calls (see the section Using JNI with COBOL in the chapter Calling Procedural COBOL from Java).
object[]   POINTER Object reference to instance of class jarray An array of Java objects. The pointer returned to procedural COBOL can be used with JNI calls (see the section Using JNI with COBOL in the chapter Calling Procedural COBOL from Java). jarray is an Object COBOL class for accessing the contents of Java arrays, and is described in the section Using the Jarray Class.
DataType   Structure Structure Complex data structure

5.3 Using the Jarray Class

The jarray class provides an Object COBOL wrapper for manipulating Java arrays. It is fully documented in the section Java Domain Class Library in your Class Library Reference.

The following COBOL program gets an array from a Java object, finds its dimensions, and displays the contents of the array.

$set ooctrl(+p-f)
 Program-id. ReadArray.
 class-control.
     arraydemo is class "$Java$arraydemo"
     .

 thread-local-storage section.
 01 aJavaObj             object reference.

 01 theTotal        pic 9(9).
 01 CDims           pic x(4) comp-5.
 01 Dims.
   03 Dims-entry    pic x(4) comp-5 occurs 256.
 01 Bounds.
   03 Bounds-entry  pic x(4) comp-5 occurs 256.
 01 ind0            pic x(4) comp-5.
 01 ind1            pic x(4) comp-5.
 01 arrayElement    pic x(4) comp-5.
 01 wsTable         object reference.
 01 wsResult        pic x(4) comp-5.

 procedure division.
   invoke arraydemo "new" returning aJavaObj
   invoke aJavaObj "getArray" returning wsTable

*> find out the number of elements in the array
     invoke wsTable "getDimensions" returning CDims
     display "The array has " CDims " dimension(s)"

*> get the number of elements in each dimension
     display "dimensions are " with no advancing
     invoke wsTable "getBounds" using Bounds
     perform varying ind0 from 1 by 1 until ind0 > CDims
          display Bounds-entry(ind0) with no advancing
          if ind0 < CDims
             display " by " with no advancing
          end-if
     end-perform
     display " "

*> display each element in the array
     perform varying ind0 from 0 by 1
             until ind0 = Bounds-entry(1)
         move ind0 to Dims-entry(1)
         perform varying ind1 from 0 by 1
                   until ind1 = Bounds-entry(2)
             move ind1 to Dims-entry(2)
             invoke wsTable "getElement" using
                             by value CDims
                             by reference Dims
                             by reference arrayElement
             display "Element " ind0 ","
                   ind1 " is " arrayElement
*> modify the contents of the array
             add 50 to arrayElement
             invoke wsTable "putElement" using
                             by value CDims
                             by reference Dims
                             by reference arrayElement
         end-perform
     end-perform

This is an implementation of the Java arraydemo class used by ReadArray. This program creates a two-dimensional array.

import mfcobol.*;
import java.io.*;

public class arraydemo extends runtime
{
    int myArray[][];

    public arraydemo()
    {
        myArray = new int[5][2];
        int i,j;
        for (i = 0; i < 5; i++)
        {
            for (j = 0; j < 2; j++)
                myArray[i][j] = i * 100 + j;
        }
    }

    public Object[] getArray()
    {
        int i,j;
        Object[] params = myArray;
        return params ;
    }
}

5.4 Using Structures

COBOL programs and Object COBOL methods can use structures in their Linkage Sections and be passed data from a Java class, providing that the corresponding Java parameter is an Object which implements the mfcobol.lang.Datatype interface, as described below:

package mfcobol.lang;
public interface DataType
{
     void synchronizeData();
     byte[] getBytes();
}

Using mfcobol.lang.Datatype enables you to pass non-simple data into a COBOL program or method without needing to rearchitect the interface with numerous elementary data types.

An example of a class that implements the mfcobol.lang.Datatype interface is mfcobol.lang.Pointer, which is defined in mfcobol.jar. It has the following constructors:

/* Create a Pointer object from String initString*/
	 public Pointer(String initString);
/* Create a Pointer object from StringBuffer initString */
	 public Pointer(StringBuffer initString);
/* Create a pointer object containing 'capacity' bytes,
	 space filling if initString has fewer characters than 'capacity' */

public Pointer(String initString, int capacity);

Server Express includes two versions of a demonstration program called RecordDemo that illustrates the use of passing data from Java to COBOL structures. These demonstration programs are contained in directories beneath $COBDIR/demo/java/oocobol and $COBDIR/demo/java/cobol. Each directory includes all relevant program files, as well as a text file for each demonstration to explain the program in more detail. The text files have names of the form demonstration-name.txt, where demonstration-name is the name of the demonstration program.


Copyright © 2002 Micro Focus International Limited. All rights reserved.
This document and the proprietary marks and names used herein are protected by international law.


PreviousCalling Object COBOL from Java Client/Server BindingNext