Tuesday, January 7, 2014

Data Structures

RPG has the ability to define data structures.  These definitions often involve defining overlapping fields and or redefining data types of those fields.   Java basic data types has no exact equivalence, but instead this type of behavior can be handled using a class definition which uses properties and methods to handle the overlap and type redefinition.


Example of a RPG DS.


Such might be handled in Java as follows.


public class datastruct1 {

   private StringBuffer XSGDTA = new StringBuffer();
   
 public String getXSGDTA() {
  return XSGDTA.toString();
 }
 public void setXSGDTA(String xSGDTA) {
  XSGDTA.insert(0,xSGDTA);
 }
 public String getMSGDT1() {
  return XSGDTA.substring(0, 2);
 }
 public void setMSGDT1(String mSGDT1) {
  XSGDTA.insert(0, mSGDT1);
 }
 public String getMSGDT2() {
  return XSGDTA.substring(2, 4);
 }
 public void setMSGDT2(String mSGDT2) {
  XSGDTA.insert(2, mSGDT2);
 }

Saturday, January 4, 2014

Data types

RPG and Java have differences in Data types. This too requires an adjustment in perspective.  iSeries is primarily a EBCDIC based platform, where as JAVA is an UNICODE based platform.  And such a difference affects issues such as space requirements and collating sequence.  Unicode is ASCII based so therefore the numbers 0-9 are a lower binary value than the alphabet, whereas in EBCDIC the numbers are a higher binary value.. So if your RPG program tests for a value being LESS than '0' to determine if it's numeric or alpha the opposite logic would apply in Java.

Furthermore, UNICODE is a double byte character system.  This means each alpha character in Java actually takes two bytes instead of one.   This generally only presents itself as a problem when dealing with data structures, which I'll address in another post.

For now the basic RPG-Java data x-reference is as follows.



Character

In RPG a Character field is generally one of fixed length.  There are variable length fields in RPG, but this is a rather new introduction.    Java Strings are much like RPG variable length fields.  Keep in mind that these Strings are storing characters in UNICODE.   

Sometimes we have a situation where we only need the value of a SINGLE Character.. Java does have a primitive data type called 'char'.   This is a single UNICODE Character. Java treats primitives such as char, int, byte differently than it does complex classes like String.   A review of the String class can be valuable.


Graphic

In RPG Graphic fields represent a string of binary information.   Java can accommodate this same concept in a Stirng class or use of a byte array.   Byte arrays are arrays of binary bytes (rather than Unicode) and thus can preserve the actual binary value of the data.

UNICODE

In RPG there has been provided a Unicode data type, which is much like a Double Byte Character field.   This is obviously handled by the String class, which stores it's characters in Unicode.

Binary, Integer, Unsigned Integer

RPG Binary data types come in sizes of 1, 2, 4 and 8 byte lengths.  These are represented in Java in either byte (1 byte, 8 bits) ,  short (2 bytes, 16 bits), int (4 bytes, 32 bits) or long (8 bytes, 64 bits) .

Date, Time, Timestamp

RPG provides for storing data in a Date or Timestamp format.   Java has an equivalent but it's not persisted in memory the same way.  Java employs a Date class or a Calendar class that has various methods for storing and modifying date / time stamp values.  See the post on working with dates in Java.

Packed Decimal, Zoned Decimal

In the business world it often very important to manage pennies precisely.  In the world from which Java comes from there is no exact replication of Packed or Zoned Decimal and that such numeric precision is managed with the BigDecimal and BigInteger classes.   These classes store the number in a byte array and then have methods to work with the data.   It's important to note that BigDecimal and BigInteger like Strings are not simple primitive data types , but instead a complex class.  And there is no distinguished difference between Packed and Zoned.

Indicator   

RPG indicators fields are equivalent to Java Boolean primitive data types.


Float

RPG has two float lengths 4 bytes, and 8 bytes.  These are represented in Java by the float and double data types.


Pointers

By design Java has NO pointers.  This is kind of a misnomer, as all Java properties are pointers, but the pointers value can not be altered, so in essence there is no pointer data types.

The Basics

RPG refers to Report Program Generator.   A business programming language originally developed for the IBM 1401 system.. Today's modern RPG only vaguely resembles it origin, but despite modern advances it still now lags behind in the forward thinking computing paradigms which Java is a part.

Java on the other hand is a fairly new language relative to RPG.  It employs OO (object Oriented) architecture.   It's this fact that makes a transition to Java for some RPG programmers a challenge.

RPG by it's very nature is a top down, procedural language, and this doesn't always translate well into a OO perspective.. Yet, don't let that dissuade you from proceeding further.. The fact is there is a great deal of similarity if you approach the transition in the right frame of reference.   So let's start with those references.

Java like RPG on the iSeries is a compiled language.   The results of the compile of either is an object which contains execution instructions for a VIRTUAL Machine.   Neither compile code that is tied to a specific piece of hardware.

What is Java.. Java is a language that utilizes a concept of a "Java Virtual Machine".. this is called the JVM.  A JVM is written to support different hardware platforms.  Whether or not Java will run on a given machine depends on whether or not there is a JVM for that machine.  For the Intel Platform, JVM's are plentiful.  There is a JVM for virtually all of IBM equipment.

Now this concept shouldn't be to strange for iSeries programmers.. For RPG itself is compiled to run on a virtual machine as well.   The iSeries is essentially a VM.. and it's interface is MI (Machine Interface code).   The MI on an iSeries is an equivalent to the JVM Byte codes of the Java platform.

Next is a list of typical RPG iSeries entities and the corresponding Java entity