Confirm behavior of %NULLIND

121 Views Asked by At

I have this RPG code that I have been trying to convert to Java, but I still don't understand how it is going to look like in Java.

RPG Code:

 C                   EVAL      %NULLIND(XXXXXXXX) = *OFF                     Birthday    
 C                   MOVEL     *BLANK        XXXXXXXX                        Birthday    
 C                   EVAL      %NULLIND(XXXXXXXX) = *ON                      Birthday    

Understanding:

  1. Set the NULL-indicator of XXXXXXXX to *OFF so that we can change the value of XXXXXXXX
  2. Set the value of XXXXXXXX to blank
  3. Set the value of XXXXXXXX to null

Is my understanding correct?

3

There are 3 best solutions below

0
jmarkmurphy On BEST ANSWER

There is a fundamental difference between Java and RPG here.

RPG variables are labels that point to a specific place in memory. They are of a specific type and contain data of that type. They also have a null indicator that is on or off. If the null indicator is off, the variable is accessible. If the null indicator is on, the variable is not accessible, however the variable remains of the same type, and the label continues to point to the same place in memory.

Java object variables, primitives cannot be null, either contain an object reference or a null. If that object variable contains a null, there is no object reference. That is, there is nothing to set to blank. It is the object that contains values. This means that it makes no sense to try to set the backing value to blank before setting the object variable to null. In fact setting a variable, that previously contained an object reference, to null disconnects that object, and potentially makes the object available to be garbage collected.

So in your example, the first two assignments are superfluous, and can be ignored. The final assignment becomes XXXXXXXXXX = null; in Java. XXXXXXXXXX has no backing object to set to blank, and if, as is implied by XXXXXXXXXX = *Blank, is defined as a String, after XXXXXXXXXX = null;, XXXXXXXXXX no longer contains a reference to a String until a String object is once again assigned to it.

tldr;

A bit of additional information here, Strings are final and immutable. A string object once created, cannot be changed. It is replaced by another string object.

This code

String str = "";
str = null;

Creates an object reference named str and a String object that contains the value "". It then assigns a reference to the String object to the variable str. The next statement then assigns a null reference to the variable str. The String object created earlier may be garbage collected, or it might hang out to be used again. If later str = "1"; is specified, a different object reference is assigned to str.

The following code:

String str = "1";
str = "2";
str = null;

contains two objects and a reference. 'str' is first assigned a reference that points to the String object that contains "1", it is then assigned a different reference that points to the String object that contains "2", and finally str is assigned a null. At the end, str is not associated with either object. And because strings are immutable, neither object could be changed to blank, even if you wanted to. The closest you could get would be to assign a reference to yet another object that contains "".

1
Charles On

You understanding is correct.

Note however, I'd call this a hack work-a-round for a process further downstream that doesn't properly handle null values.

In addition to storing a numeric value in a character field apparently.

Basically, something somewhere isn't checking for NULL, and instead of fixing that the developer decided to ensure that the field contains blanks in addition to having the NULL indicator on.

0
Barbara Morris On

I think RPG's %NULLIND is more related to the SQL concept of "null-capable" than the Java concept of a null object.

I am not very familiar with using Java to access database, but I think that if the Java code is using ResultSet, then it would use updateNull() to set a column to null and wasNull() to check whether the column was null.

The ResultSet documentation is here: https://docs.oracle.com/javase/6/docs/api/java/sql/ResultSet.html

I found that documentation by following the java.sql.package link in this IBM docs page: : https://www.ibm.com/docs/en/i/7.5?topic=preparedstatements-creating-using.