If you have a String object in your RPG code, you can retrieve its length and contents using the code in Figure 88.
D stringBytes PR 100A VARYING
D EXTPROC(*JAVA
D : 'java.lang.String'
D : 'getBytes')
D stringLength PR like(jint)
D EXTPROC(*JAVA
D : 'java.lang.String'
D : 'length')
D string S like(jstring)
D len S like(jint)
D data S 100A VARYING
/free len = stringLength (string);
data = stringBytes (string);
if (len > %len(data));
error ('Actual string was too long');
endif;
/end-free
You can define the returned value from the getBytes method as character data of any length, either varying or non-varying, choosing the length based on your own knowledge of the length of data in the Java™ String. You can also define the return value as a Date, Time or Timestamp, if you are sure that the String object will have the correct format.
Alternately, you can retrieve the string value as a UCS-2 value, by calling the getChars method instead of getBytes.