PreviousCalling Object COBOL from Java Combination ActiveX/Java ClassesNext

Chapter 8: Java Data Types

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

8.1 Overview

The Java language defines its own data types, which are different to 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 the diagram below.

Mapping data between COBOL and Java

Fig 8-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.

8.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 chapter Calling Procedural COBOL from Java) or the Object COBOL Java domain (see 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 type column. You can find javatypes.cpy in your Net Express/source directory.

Java User defined COBOL Object COBOL 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 ref 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 All characters in Java are represented by 2-byte Unicode characters
float jfloat comp-1 comp-1 (by ref only on Unix) Floating-point number
double jdouble comp-2 comp-2 by ref 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 the length of the string currently held in the buffer.
StringBuffer
objects 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 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 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 Jarrays.

8.3 Using Jarrays

The jarray class provides an Object COBOL wrapper for manipulating Java arrays. It is fully documented in the Java Domain Class Library Reference. Click Help Topics on the Net Express Help menu, and then click Reference, OO Class Library, Class Library Reference, and click the shortcut to start the 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 ;
    }
}


Copyright © 2000 MERANT 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 Combination ActiveX/Java ClassesNext