This chapter describes how COBOL and Java data types are mapped on to each other.
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 Figure 0-1.
Figure 1: Mapping data between COBOL and Java
Numeric data types are converted in the same way whether the COBOL program is a procedural program, or OO COBOL. Objects and strings are converted differently, depending on whether you are using procedural COBOL or OO COBOL.
OO COBOL handles Java objects as COBOL object references, but procedural COBOL handles them as pointers.
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 OO COBOL Java domain (see the chapters Calling Java from COBOL and Calling OO COBOL from Java). The COBOL column in the table shows you the conversion rules for procedural COBOL, and the OO COBOL column shows you the rules for the OO COBOL Java domain.
Java Data Type | COBOL Data Type Used with .cobcall(...) | OO COBOL Data Type Used with .cobinvoke() | Description |
---|---|---|---|
byte | pic s99 comp-5 | pic s99 comp-5
pic s99 comp |
Signed 1-byte integer |
short | pic s9(4) comp-5 | pic s9(4) comp-5
pic s9(4) comp |
Signed 2-byte integer |
int | pic s9(9) comp-5 | pic s9(9) comp-5
pic s9(9) comp |
Signed 4-byte integer |
long | pic s9(18) comp-5 | pic s9(18) comp-5
pic s9(18) comp 1 |
Signed 8-byte integer |
boolean | pic 99 comp-5 | pic 99 comp-5 | Zero value is false, non-zero is true |
char (Unicode) | pic 9(4) comp-5 | pic s9(4) comp | All characters in Java are represented by 2-byte Unicode characters |
float | comp-11 | comp-1 1 | Floating-point number |
double | comp-2 1 | comp-2 1 | Double-precision floating-point number |
String | mf-jstring 2
pointer 3 |
pic x(n) | The capacity is always zero. You should consider a string passed into a COBOL program as read-only, and not to be amended. |
StringBuffer | mf-jstring 2
pointer 3 |
pic x(n) | The capacity is the total size of the buffer, and the size the length of the string currently held in the buffer. |
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 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 OO 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 |
Comp3 | comp-3 | comp-3 | COMP(UTATIONAL)-3 or Packed-Decimal |
Pointer(StringBuffer)
Pointer(String, int capacity) |
pic x(99) | pic x(99) | Wraps a String, StringBuffer so it can safely be
passed to COBOL with the right size allocated for the receiving COBOL to modify
the Java object.
StringBuffer capacity is used as the largest possible length that COBOL can edit. |
java.sql.Date(long)
CobolDate(date) |
0x jyear pic 9(4).
0x filler pic x. 0x jmonth pic 99. 0x filler pic x. 0x jday pic 99. |
same as for cobcall | Standard SQL Date type as used by JDBC |
java.sql.time(long)
CobolTime(time) |
0x jhour pic 99.
0x filler pic x. 0x jminute pic 99. 0x filler pic x. 0x jseconds pic 99. |
same as for cobcall | Standard SQL Time type as used by JDBC |
CobolBigDecimal(java.math.BigDecimal) |
pic s9(19)v9(19) comp-3 | same as for cobcall | The CobolBigDecimal wrapper class enables COBOL to update the value of a BigDecimal. The constructor for CobolBigDecimal() takes an initial value and the updated value can be obtained by using the .getValue() method. |
CobolBigInteger(java.math.BigInteger) |
pic S9(38) comp-3 | same as for cobcall | The CobolBigInteger wrapper class enables COBOL to update the value of a BigInteger. The constructor for CobolBigInteger() takes an initial value and the updated value can be obtained by using the .getValue() method. |
CustomRecord | Structure | Structure | Complex data structure represented in Java as an array of objects. |
CobolNational | pic x(n) usage is national. | same as for cobcall | This java class enables you to create UTF-16 strings that you can pass to COBOL. |
Footnotes:
The parameter can be passed only by reference. For floating point data types this restriction applies to UNIX platforms only. For long data types on 32-bit platforms the parameter can be passed by reference only, and on 64-bit platforms by reference or by value only.
Use mf-jstring for .cobcall(...). mf-jstring requires the receiving COBOL program to use ptr2string pointer to access the Java String or StringBuffer. Using mf-jstring gives you access to the size and capacity of the Java object.
Use pointer for .cobcall(...).
The conversion of COBOL data types that are not listed in the COBOL or OO COBOL columns of the above table is not supported. If you need to use a COBOL data item of a type not listed above, use an intermediate data item of a supported type and move your data to and from the intermediate data item as necessary.
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. You can find the copyfile in your $COBDIR/cpylib directory.
These typedefs all start with the prefix "j" and are shown in the following table:
Typedef | COBOL Type |
---|---|
jbyte | pic s99 comp-5 |
jshort | pic s9(4) comp-5 |
jint | pic s9(9) comp-5 |
jlong | pic s9(18) comp-5 |
jboolean
JNI-TRUE is true JNI-FALSE is false |
pic 99 comp-5 |
jchar | pic 9(4) comp-5 |
jfloat | comp-1 |
jdouble | comp-2 |
jobject | pointer |
jbigdecimal | pic s9(19)v9(19) comp-3 4 |
jbiginteger | pic s9(38) comp-3 4 |
mf-sql-date
(in javatypes.cpy) |
0x jyear pic 9(4).
0x filler pic x. 0x jmonth pic 99. 0x filler pic x. 0x jday pic 99. |
mf-sql-time
(in javatypes.cpy) |
0x jhour pic 99.
0x filler pic x. 0x jminute pic 99. 0x filler pic x. 0x jseconds pic 99. |
Footnotes:
All programs that use these typedefs must be compiled with the directive INTLEVEL 4
The jarray class provides an OO COBOL wrapper for manipulating Java arrays. It is fully documented in the Java Run-time Class Library Reference, which is in docs/mfcobol.docs.zip.
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 com.microfocus.cobol.*; import java.io.*; public class arraydemo extends RuntimeSystem { 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 int[][] getArray() { return myArray; } }
The ParameterList() class in com.microfocus.cobol.lang helps with coding parameters being passed between Java and COBOL.
The general mechanism for using ParameterList() is:
RuntimeSystem.cobcall("myProgram", new ParameterList() .add(myOneInt,RuntimeSystem.BY_VALUE) .add(mySecondParameter,RuntimeSystem.BY_REFERENCE) .add(99) // 99 by reference );
Where the parameters are:
myProgram | Tthe program being called. |
myOneInt | An integer parameter being passed by value. |
mySecondParameter | A parameter being passed by reference. |
99 | A numeric value being passed by reference. |
COBOL programs and OO 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 com.microfocus.cobol.lang.Datatype interface or com.microfocus.cobol.lang.CustomRecord, as described below:
package com.microfocus.cobol.lang; public interface DataType { void synchronizeData(); byte[] getBytes(); }
and
package com.microfocus.cobol.lang; public interface CustomRecord { public Object[] getParameters(); public void setParameters(Object[] parms); }
Using com.microfocus.cobol.lang.Datatype/com.microfocus.cobol.lang.CustomRecord 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 com.microfocus.cobol.lang.Datatype interface is com.microfocus.cobol.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);
Custom records are useful for passing group items to existing COBOL programs. You use then in conjunction with the .cobcall mechanism.
The CustomRecord interface is:
package com.microfocus.cobol.lang; public interface CustomRecord { public Object[] getParameters(); public void setParameters(Object[] parms); }
In the RecordDemo2 customerDetails is defined as:
01 customerDetails. 03 customerName pic x(30). 03 customerAddress pic x(30). 03 customerRef pic 9(6).
A Java implementation could be:
import com.microfocus.cobol.lang.*; import java.text.*; public class RecordData implements com.microfocus.cobol.lang.CustomRecord { private String customerName; private StringBuffer customerAddress; private int customerRef; RecordData(String name, String address, int ref) { customerName = name; customerAddress = new StringBuffer(address); customerRef = ref; } public String getCustomerName() { return this.customerName; } public String getCustomerAddress() { return this.customerAddress.toString(); } public int getCustomerRef() { return this.customerRef; } public Object[] getParameters() { String strCustomerRef = Integer.toString(this.customerRef); while(strCustomerRef.length() < 6) { strCustomerRef = "0"+strCustomerRef; } customerAddress.setLength(30); /* must ensure length is right! */ customerAddress.ensureCapacity(30); return new ParameterList() .add(new Pointer(this.customerName,30)) .add(this.customerAddress) .add(strCustomerRef.getBytes()) .getArguments(); } public void setParameters(Object[] parms) { Pointer ptr = (Pointer)parms[0]; this.customerName = ptr.toString(); this.customerAddress = (StringBuffer)parms[1]; byte[] byteCustomerRef = (byte[])parms[2]; this.customerRef = Integer.parseInt(new String(byteCustomerRef)); } public String toString() { return "Customer Name : "+this.customerName+"\n"+ "Customer Address : "+this.customerAddress+"\n"+ "Customer Ref : "+this.customerRef; } }
Your COBOL development system includes demonstration programs that illustrate the use of passing data from Java to COBOL structures. The folders $COBDIR/demo/javademo/oocobol/record and $COBDIR/demo/javademo/cobol/record in your product installation contain all relevant files for the demonstration programs, as well as readme.txt files to explain the programs in detail.