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:
XXXXXXXX
to *OFF
so that we can change the value of XXXXXXXX
XXXXXXXX
to blankXXXXXXXX
to nullIs my understanding correct?
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 "".