Mapping between COBOL and Java data types for non-OO COBOL/Java interoperability
This section describes the COBOL data types and related Java™ data types in non-OO COBOL programs that interoperate with Java through use of the JAVA-CALLABLE or JAVA-SHAREABLE directives or through use of the CALL statement to call a static Java method.
A COBOL program and a Java method need to pass data between one another under the following circumstances:
- A Java method passes arguments to a COBOL native method
- A Java method receives a returned value from a COBOL native method
- A COBOL program passes arguments to a static Java method
- A COBOL program receives a returned value from a static Java method
- A Java method directly accesses a data item in a native COBOL method's working-storage
PIC X |
byte |
PIC X(m) DISPLAY , m>1PIC N(m) NATIONAL ,
m>0PIC U(m) UTF-8 , m>0PIC U BYTE-LENGTH
m , m>0PIC X DYNAMIC [LIMIT n]
(not supported by the JAVA-SHAREABLE directive and will be ignored)PIC U DYNAMIC [LIMIT n] (not supported by the JAVA-SHAREABLE
directive and will be ignored) |
String |
PIC S9(n) COMP-5 , 1 ≤ n ≤ 4 |
short |
PIC S9(n) COMP-5 , 5 ≤ n ≤ 9 |
int |
PIC S9(n) COMP-5 , 10 ≤ n ≤ 18 |
long |
COMP-1 (4-byte IBM® hex float) |
float (4-byte IEEE float) |
COMP-2 (8-byte IBM hex float) |
double (8-byte IEEE float) |
usage COMP-3/PACKED-DECIMAL numeric |
java.math.BigDecimal |
usage DISPLAY numeric (zoned) |
java.math.BigDecimal |
|
boolean |
|
byte[m] |
where 1 ≤ n ≤ 4. |
short[m] |
where 5 ≤ n ≤ 9. |
int[m] |
where 10 ≤ n ≤ 18. |
long[m] |
|
float[m] |
|
double[m] |
|
boolean[m] |
|
BigDecimal[j] |
where m>1. |
String[n] |
|
String[n] |
|
String[m] |
Any alphanumeric group item that does not satisfy the definition of a Java-compatible array type is considered to map to a Java byte array (byte[]) and it is the Java application's responsibility to both read and set the byte array appropriately based on the corresponding COBOL group definition. | byte[] |
Alphanumeric groups
An alphanumeric group item in COBOL that does not satisfy the definition of one of the Java-compatible array types can still be used as an argument in a call to a static Java method and as a parameter in a COBOL program that is callable from Java.
When the group is used as a parameter in a Java callable program, it is assumed that the data being passed from Java to the parameter is a byte array object (byte[]), and it is the responsibility of the calling Java method to set up the data in the corresponding byte
array to match the byte layout of the receiving COBOL group item. Conversely, when the group is used as an argument in a call to a static Java method, the called Java method must assume that it is receiving a Java byte array object (byte[]), and it is the responsibility of the Java method to wrap the incoming byte array with a ByteBuffer in order to extract data out of the byte array in a meaningful way based on the layout of the bytes in the corresponding COBOL group.
Further considerations
- Alphanumeric group items are only compatible with Java when used as COBOL parameters or arguments. That is, alphanumeric group items cannot be the returned value of a java-callable program or the returned value from a call to a Java static method.
- Single-dimension arrays of each supported Java type are supported. To have the compiler treat an incoming Java argument as a single dimension array, or to treat an outgoing COBOL argument as being intended for a single dimension Java array, the COBOL parameter or argument should be defined using the following format:
01 identifier-1 occurs n times. 03 identifier-2 <java-compatibile-cobol-data-definition>.
For example, the following COBOL definition corresponds to aJava int[]
type:01 list-data occurs 10 times. 03 num pic s9(9) comp-5.
Note: If the number of elements in an incoming Java array argument does not match the number of elements indicated in the OCCURS clause of a fixed length table definition in the corresponding COBOL parameter, a runtime error will occur. - For conversions between COBOL COMP-1/COMP-2 items and Java float/double items, a loss of precision may occur due to the difference in the underlying representation. That is, COMP-1/COMP-2 are stored in IBM hex float format, and float/double are stored in IEEE binary float format:
- Conversions between IBM hexadecimal floating point format and IEEE floating point format are done via Language Environment® special purpose C/C++ interface __fp_htob(), using rounding mode _FP_HB_BRN (biased round to nearest).
- Conversions between IEEE floating point format and IBM hexadecimal floating point format are done via Language Environment special purpose C/C++ interface__fp_btoh(), using rounding mode _FP_HB_BRN (biased round to nearest).
- A BigDecimal value can be received into either a zoned or packed decimal item in COBOL. Similarly, zoned and packed decimal items are both legal senders for BigDecimal receivers in Java. The conversion between zoned/packed decimal and BigDecimal (and vice versa) is handled automatically by routines in the IBM JZOS toolkit, which must be installed in the environment in which the COBOL / Java interoperable application is running.
- Data items defined as "
PIC X(m)/U(m)/N(m)
" will be padded with an appropriate space character to a length of m characters when the length of the corresponding Java String object is smaller than m characters. Data items defined as "PIC U BYTE-LENGTH m
" will be padded with an appropriate space character to a maximum length of m bytes when the length of the corresponding Java String object is smaller than m bytes. Data items defined as "PIC X/U DYNAMIC [LIMIT n]
" will not be padded with spaces and will have the same byte length as the corresponding Java String object, up to a maximum of n bytes if theLIMIT
phrase of theDYNAMIC
clause is specified.