Welcome!

Java Authors: Sharon Barkai, Elizabeth White, Liz McMillan, Pat Romanski, Kevin Benedict

Related Topics: Java

Java: Article

NetRexx Programming for the JVM

NetRexx Programming for the JVM

What This Series Is About
This article is Part 3 of an interactive series that discusses the many languages that compile and/or run on the Java platform. Java Developer's Journal invites you to vote for your favorite non-Java programming language in the JDJ forum. Your vote will decide which languages will be covered by the series, and in what order. A lot of languages work in the JVM, but this series will cover only the most popular, as determined by your votes. To vote:

  • Go to the top of the JDJ Web page and click the forum graphic.
  • Click the "Enter the JDJ Developer's Forum" link.
  • Go to the Java (Writer's Forum) section.
  • Click on the link that says "Vote for your Favorite Language that runs in the JVM."
This column is the resting place for non-Java-language, JVM-related topics for JDJ. It focuses on topics such as:
  • Creating JavaServer Pages (JSP) in JavaScript, Webl and Python
  • Integrating with Tcl, Python and Perl scripts
  • SWIG for Java
  • Open source Java initiatives like EnHydra and Apache Tomcata
  • COM/DCOM from pure Java
  • CORBA to Legacy integration, etc.
Let us know what else you want to see covered.

In the first two articles in this series Java was presented as the system language, and the higher-level language was presented as the glue language. This allows you to define frameworks, libraries and components in Java and glue them together to make applications. This was described in detail in the February JDJ (Vol. 5, issue 2).

Though it wasn't on the first suggested list, NetRexx received a lot of votes. Those votes count, as evidenced by the fact that NetRexx is covered here - the last time I checked it was running neck and neck with JPython.

NetRexx 101
NetRexx from IBM is a human-oriented language that's geared to Java. It marries REXX, one of the best-known scripting languages, with Java. Thus you get the readability and ease of use of REXX with the universal Java platform. NetRexx compiles to Java bytecode. Its claim to fame is quickly developed, maintainable code.

"NetRexx is a human-oriented programming language that makes writing and using Java classes quicker and easier than writing in Java," from www2.hursley.ibm.com/netrexx/. NetRexx is a REXX variant; REXX is a popular scripting language for IBM operating systems like OS/2 and increasingly for other platforms as well.

NetRexx is a lot like Java, although, unlike JPython, not more dynamic than Java. Instead, it's an easier syntax that makes system programming easier. NetRexx mixes nicely with Java - for example, it can subclass Java classes and implement interfaces. Also unlike JPython, which augments Java, NetRexx takes Java on head to head as an easy-to-use systems language while maintaining some of the features that made REXX programming popular. At first this may seem to put NetRexx at a disadvantage. However, NetRexx does a nice job of being a better REXX than REXX and a better Java than Java.

What does human-oriented mean? I was originally fooled into believing that NetRexx was a scripting language, like its parent. It isn't. It's described as making things easier on the programmer and harder on the compiler. You have the option of running NetRexx in a mode that's more like a scripting language or more like a systems language. This option manifests itself in the NetRexx syntax via the option keyword (similar to the option keyword in Visual Basic).

NetRexx advocates claim NetRexx is easier to learn than Java. I tend to agree, with some reservations. If you're a novice programmer, NetRexx is easier to learn than Java. Also, if you come from a Python, JavaScript, Visual Basic or Delphi background, then NetRexx would be easier to learn than Java. However, if you're from a C++ background, Java is probably easier to learn. Of course, if you're from a REXX or ObjectRexx background...well, you get the picture.

I found NetRexx extremely easy to learn and use. And the code I created was easier to read than the equivalent Java code. NetRexx has every language feature that Java has, plus some additional features that make JavaBean development easier.

Rosetta Stone
For comparison, each NetRexx sample application will have a corresponding Java implementation. This article covers the following sample applications (last month we compared JPython to Java):

  • A simple GUI application
  • A simple statistics application
  • Embedding the script into an application (if applicable)
  • A simple example parsing text
NetRexx 101: A Simple Class
Listing 1 is a sample class in NetRexx and Listing 2 is the equivalent class in Java.

Use of the NetRexx class is the same as for the Java class. Let's break this down and compare it to the Java equivalent. To declare private instance variables in Java you'd do this:

private String firstName, lastName;
private int id, dept;

In NetRexx you'd do this:

Properties Private
firstName=String
lastName=String
id=int
dept=int

To create a bean property in Java you'd do this:

private Employee manager;

public Employee getManager(){
return manager;
}

public void setManager(Employee manager){
this.manager=manager;
}

In NetRexx you'd do this:

Properties indirect
manager=Employee

As you can see, NetRexx is much less verbose. The handling of properties reminds me of Delphi.

Instance variables and class variables are called properties in NetRexx. Note two things: (1) the use of the indirect keyword with properties, and (2) the call to ron.getManager(). The indirect keyword is a way to declare private variables that can be accessed outside of the class. In other words, the indirect keyword is shorthand for Java properties.

There are four forms of visibility for instance variables in NetRexx: public, indirect, inheritable and private. The NetRexx public and private visibilities are just like their Java counterparts. The inheritable visibility is like Java's protected visibility, and the indirect provides bean properties.

The indirect automatically provides assessor methods via the JavaBean "design patterns" for bean properties. If you add your own getter and setter methods to the class, they override the default. Actually, if you override your own, NetRexx won't generate the default assessor methods for that property.

Compare the listing for the NetRexx Employee class to the listing for the Java Employee class. The former class is shorter and in my opinion more readable. Can you imagine a class with five to 10 bean properties? The NetRexx class is significantly shorter.

NetRexx also has support for indexed properties. It takes up to four Java methods to do what one indexed property declaration does! If you use properties a lot, NetRexx is an easy sell.

To create an instance of an Employee and print it to the screen, you'd do the following:

say Employee()

The equivalent Java statement would be:

System.out.println(new Employee());

Next we create two instances of employee called Joe and Ron and print them to the console. We print Joe, who is Ron's manager, by invoking the getManager method of Ron. First in NetRexx, then in Java:

NetRexx:
say Employee()
joe = Employee Employee("Joe", "Batista", 100, null, 1);
ron = Employee("Ron", "Furgeson", 101, joe, 1);
say ron
say ron.getManager()

Java:
Employee joe = new Employee("Joe", "Batista", 100, null, 1);
Employee ron = new Employee("Ron", "Furgeson", 101, joe, 1);
System.out.println(ron);
System.out.println(ron.getManager());

The syntax of NetRexx is close to Java (but actually closer to Python). One feature NetRexx has is default values. As mentioned in the March article on JPython, this feature can save some coding effort, not to mention some headaches. Have you ever had several different versions of the same method? And you just wanted to have different default values? Every default value is another overloaded method, which can get messy. I like that NetRexx has default values.

Rosetta Stone GUI
Now that we've created a simple class, we'll create a simple GUI. The employee class and the employee form examples are nonsensical - the idea is to demonstrate and compare NetRexx to Java. In the last article we implemented the EmployeeForm example in both Java and JPython, so you can compare NetRexx side by side with Java and JPython.

In the JPython article we covered the development of the GUI in the interactive interpreter, which is great for prototyping. This isn't currently possible with NetRexx, but Mike Cowlishaw, creator of NetRexx, is adding an interactive interpreter to NetRexx as the next feature.

The EmployeeForm is simple: it has two text fields and an OK button. The first text field is used for the name. The user enters the first and last names in the first text field. When the user hits the OK button, the EmployeeForm handleOkay method parses the text for the first and last name. Listings 3 and 4 show the NetRexx and Java versions of EmployeeForm.

The keywords are similar between the Java and NetRexx versions. For example, they both use import-to-import classes. A couple of key differences will be highlighted.

NetRexx has inner classes, called minor classes in NetRexx-speak. However, it doesn't have the concept of anonymous inner classes - I like anonymous classes for handling methods. Thus I had the EmployeeForm implement the ActionListener in the NetRexx version. However, in the Java version I used an inner class.

Thus the NetRexx event handling looks like this:

class EmployeeForm extends JFrame implements ActionListener
...
method EmployeeForm
...
okay.addActionListener(this)
...

method actionPerformed(event=ActionEvent)
handleOkay()

While the Java version looks like this:

public EmployeeForm(){
...
okay.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent event){
handleOkay();
}
});

Of the two approaches I prefer Java because I don't like to clutter the interface of my class with unnecessary methods (like actionPerformed). I could have declared an inner (minor) class in the NetRexx version and then used the minor class as the event handler, but I honestly don't think I'd bother. I like anonymous inner classes and am quite spoiled using them.

The Java and NetRexx versions of the EmployeeForm parse the string from the name text field in entirely different ways. The latter uses the REXX object, which is a cross between a string, a collection, an associative array (like Perl) and a generic data type. I'm not sure if I love the REXX object or hate it, but it sure makes parsing strings easy. Here's the NetRexx version of parsing the name.

_name = Rexx(this.name.getText())
fname = _name.word(1)
lname = _name.word(2)

Compare the ease of the above approach to the Java version of the same routine.

name = this.name.getText();
index = name.indexOf(" ");
fname = name.substring(0, index);
lname = name.substring(index+1, name.length());

Of the two, I prefer the NetRexx approach - the REXX object is growing on me.

Rosetta Stone Statistics
Just to highlight how well the language can do simple, common things, we'll create an easy application that calculates statistics for house prices in a neighborhood. We'll create a program that gives a list of number finds: the averages (mean, mode, median) and range. We'll list each function's code, then break it down and describe the function line by line.

Implementing getRange
Since getRange is the easiest, we'll do it first. Essentially, we want a function that returns the minimum/maximum values in a list and their range.

First try implementing getRange (see Listing 5), which uses the java.util.Collections class to get minimum and maximum value. When the getRange function is done it returns the min, max and the range in an ArrayList instance.

Thus, to declare the min, max and ranges variable in Java, do this:

double min, max;
ArrayList ranges;

In NetRexx, do this:

min=double
max=double
ranges=ArrayList

Note that in NetRexx declaring the variable before assigning it is an optional step, as we'll show later.

To get the value min and max values from the min and max methods of the java.util.Collections class, do this in Java:

min = ((Double)Collections.min(nums)).doubleValue();
max = ((Double)Collections.max(nums)).doubleValue();

In NetRexx you'd do this:

min = ( Double Collections.min(nums)).doubleValue()
max = ( Double Collections.max(nums)).doubleValue()

This is fairly similar to the Java way. (The Java versions of Listings 5-8 can be found on the JDJ Web site in the March 2000 [Vol. 5, issue 3] digital edition, www.JavaDevelopersJournal.com.)

Implementing getMean
The getMean function figures out the mean of a sequence of numbers. It iterates through the list, adds all the values together and stores them in a sum. It then figures the mean by dividing the sum divided by the length of the sequence of numbers. The getMean sample (see Listing 6) uses an argument called sample to determine if this is a sample mean or a population mean.

This example shows sample usage of:

  • Loop while
  • If and else statements
  • Default argument
Let's break down the getMean function step by step. First notice the method declaration:

method getMean(nums=ArrayList, sample=boolean 1) public static returns double

This method is passed an ArrayList (nums) and a boolean flag (sample). Notice that the visibility and modifiers are at the end. Also notice that the sample argument has a default value. Thus, if the second argument isn't specified when calling this method, the value of 1 (true) will be implied.

The equivalent Java code would look like this:

public static double getMean (ArrayList nums){
getMean (nums, true);
}

public static double getMean (ArrayList nums, boolean sample){
...
}

In Java you'd have to declare two methods. As you can see, if you had a method with four default values, the equivalent Java code could get quite verbose.

The getMean method defines three local variables called average, sum and value, which hold the sum, average and current iteration value.

value=Double
sum=0.0
average=0.0

To iterate through the ArrayList we need to get the iterator. The method gets an iterator from the ArrayList argument passed to the method as follows:

iterator = nums.iterator()

Notice that we didn't declare the iterator first.

Now we can use the iterator to iterate through the numbers in the nums ArrayList while adding the numbers together.

loop while iterator.hasNext()
value = Double iterator.next()
sum = sum + value.doubleValue()
end

To cast the value from the iterator.next from an Object into a java.lang.Double we do this:

value = Double iterator.next()

The equivalent Java code would look something like this:

value = (Double) iterator.next();

Next, the getMean method checks to see if this is a sample mean. If it is, the method figures the average by dividing the sum by the number of items in nums less one, or else it divides the sum by the number of items in the nums ArrayList.

-- Check to see if this is a sample mean
if sample then
average = sum / nums.size()-1
else,
average = sum / nums.size()

Last, the method returns the average:

return average

The foregoing is not much different from what you'd do in Java. It's actually very similar. In fact, you could probably read and understand the NetRexx code without much coaching.

Implementing getMode
The getMode function (see Listing 7) finds the value that repeats the most. (Note: This is not a complete implementation of mode because it works only with discrete values.) The first thing this function does is duplicate the ArrayList because it's going to modify it. Then the method iterates through the items in the nums sequence and counts the number of occurrences of the current items (we use the built-in sequence method count). Once we count an item, we remove it from the duplicated sequence.

This example shows example usage of:

  • Loop while
  • If statement
Because of space constraints we won't cover getMode step by step. Please compare it to the Java version of getMode.

Implementing getMedian
The getMedian function (see Listing 8) finds the middle-most value once the sequence is sorted.

Listing 8 shows example usage of:

  • The modulus operator (%)
  • If and else statements

Comparing Java to NetRexx
The functionality of the rest of the code for this example is very similar. The NetRexx version is typically less verbose than the Java version, but unlike Python there aren't a lot of features in the NetRexx example to make this development significantly shorter or different. Compare Listing 9, the Java version of this statistics package, to Listing 10, the NetRexx version. Listings 9 an 10 can be downloaded from the JDJ Web site, www.JavaDevelopersJournal.com.

Notice that in the NetRexx version the public static methods don't have to be inside a class declaration. If static methods are defined outside a class definition, the methods are treated as though they're part of a class definition corresponding to the name of the source file. Notice also that the RunReport isn't in a static main method. If code is in a file outside a method declaration, the code is assumed to be in the main method. These features make writing scripts easier and less verbose.

Vote for Your Favorite
There are a lot of 100% pure programming languages that work in the JVM, and JDJ wants your opinion of which are the best, and why. An example list follows: JPython, NetRexx, Rhino, Instant Basic, Jacl, BeanShell, Pnuts, Bistro and Kawa (Lisp/Scheme-like).

Scorecard
How does NetRexx score? Here's my opinion.

AUTHOR'S SCORECARD FOR NETREXX

  • Ease of use 9
  • Embeddability ? (Not yet, maybe via Bean Scripting Framework)
  • Resemblance to parent language 8
  • Unique features 9
  • String parsing 10
  • Productivity 9
  • Working well with Java classes 10
  • Development environment/debugging 7
Let's drill down a bit on the above criteria.
  • Ease of Use: Compared to Java, I feel that NetRexx is easier to learn, use and read. I have a lot of experience working and training people who are new to Java. Java is tough if you're used to working with a higher-level language. NetRexx bridges the gap, giving developers the ease of use of a higher-level language with the power of Java. I think NetRexx hits the same sweet spot as Visual Basic, for example, a good balance of dynamic typing and ease of use, as discussed in the first article
  • Embeddability: N/A. I define embeddability as the ability to create and provide language for an application, for example, a scripting language (or a macro language). Since NetRexx doesn't have an interactive interpreter or an interpreted mode, I don't think this is applicable yet, although you could approximate it with the Bean Scripting Framework, which is also from IBM and will be covered in the next article. Mike Cowlishaw is adding an interactive interpreter, which should be out by the time you read this article.
  • Resemblance to Parent Language: It does resemble REXX, of course, but it departs drastically from ObjectRexx and favors Java-like syntax over the less popular ObjectRexx syntax. I think the departure is a good thing; it morphs NetRexx into a language that's at home in the JVM.

  • Unique Features: The so-called human-oriented features acquired from REXX give it a nice feel. REXX was designed to be easy to use, and was essentially written for nonprogrammers, that is, system administrators. This is a real advantage as the code is easier to read and understand.
  • String Parsing: String can be manipulated with the methods of the REXX class or parsed with the parse instruction. The parse instruction is a powerful instruction for string manipulation. The parse command uses templates that can consist of string literals, numbers and symbols. The template is much easier to use and learn than regular expressions, yet the template is suprisingly powerful. The parse instruction comes straight from classic REXX.
  • Productivity: As I've indicated throughout this article, NetRexx is less verbose than Java. Talk less, say more. In addition, it's easier to read and understand, which makes it easier to inherit someone else's code and get up to speed.
  • Working Well with Java Classes and APIs: You can compile NetRexx to Java bytecode. You can easily use Java APIs from NetRexx. The syntax for importing classes is very similar to that for Java. (By the way, you can develop JavaServer Pages with NetRexx via IBM's Bean Scripting Framework. You could also use JavaScript(Rhino) and Python(JPython). Cool beans!)
  • Development Environment/Debugging: I've been told by a reliable source that you can use a regular Java IDE to debug NetRexx source code, but I didn't find any instructions on the Web site describing how to do it. Since NetRexx is human-oriented, you might think that they'd provide some human-oriented instructions, but they didn't (at least none that I could find). It would be nice if IBM had a VisualAge for NetRexx or added NetRexx support to their VisualAge product. REXX and Object REXX, for example, have been good to IBM. I'd like to see IBM support this in their products.
One feature that NetRexx has that's great for debugging is the trace instruction. You can specify the trace level of a class, for example, to trace every line, method calls and more. I found this great for debugging code. Think about it. How often have you added System.out.println calls throughout your code so you could "trace" methods? This is a great feature!

Related Links
NetRexx tutorial: http://consult.cern.ch/news/netrexx/html/nr_toc.html
Main NetRexx site: www2.hursley.ibm.com/netrexx/

Random Thoughts
I install and try out a lot of software development tools and can usually make up for the lack of instructions if I'm given enough breadcrumbs. I found the NetRexx install a bit clumsy and quite easy to screw up. I figured it out, but I feel it could easily stump new users.

If NetRexx is geared for nonprogrammers or new Java programmers coming from a fancy 4GL with a fancy IDE, then its install is really lacking. There's no real install program - just a zip file with some instructions.

The NetRexx user base is strong and involved. While there are many NetRexx tutorials, most of them aren't linked to or from the main NetRexx site. And strangely enough, there's no real strong tutorial on the main NetRexx Web site, just a getting-started page and the language reference (which is very well written). This is a big gap and a real problem. There's also an IBM Redbook on NetRexx, but it seems pretty dated.

If you want to use the latest NetRexx features, you need to refer to the language reference. It would be nice if someone at IBM could loan Mike Cowlishaw a resource or two to come up with a good language tutorial. (A book on NetRexx written by Mike Cowlishaw probably fits the gap nicely.)

In short, you're going to have to hunt and peck at many sites to get a good feel for NetRexx programming. I did, and it wasn't too difficult, but I'm lazy and would like to do one-stop shopping for information like this. Also, this may throw someone who's new to NetRexx - I assume the goal is to increase the NetRexx user base, not deter new people. If you don't make it easy, they'll go somewhere else.

I like NetRexx a lot in its current incarnation but it's not different enough from Java. I think it needs to expand on cool features like indirect properties. I want to see more. I'd like to see language support for events so that one line of NetRexx code replaces the need to declare a listener interface, event class and add/remove listener methods. The event syntax could be similar to the way Visual Basic (5.0 and above) defines events for ActiveX components.

The fact that NetRexx is a new beast and doesn't have to be backward compatible with another language is good - unlike JPython, which endeavors to be compatible with Python. This gives NetRexx a real advantage. (Python has its advantages as well - numerous advantages. Read the March JDJ [Vol. 5, issue 3] to learn more about JPython.)

NetRexx needs an interpreter but Mike is taking care of that. In addition, NetRexx also needs additional language syntax for dealing with collections, iterators and so forth, á la JPython.

Last, if IBM isn't going to dedicate more resources to NetRexx, then they should really open-source it. NetRexx has a strong user base that's involved and dedicated. It would do well in the open-source world. I'd like to see IBM open-source and add more resources.

Parting Shots
NetRexx fills the gap between high-level languages and Java. It allows you to safely declare method signatures and interfaces, which is important for systems development. I didn't find NetRexx as easy to use or as dynamic as JPython; however, comparing JPython and NetRexx is like comparing apples and oranges since one is a systems language with scripting features and the other is a dynamic scripting language. I see NetRexx closing the gap on JPython, but never getting as dynamic or as feature-rich.

However, unlike JPython, which has to be compatible with Python, NetRexx doesn't have to be compatible with ObjectRexx. This gives NetRexx a real advantage as it can add new language features that make sense in the Java Virtual Machine (e.g., indirect properties).

As you may know, we had an informal poll and JPython and NetRexx were the top two languages for the JVM. I also solicited feedback from developers who had used both JPython and NetRexx.

The feedback I got was this: if you're from a REXX background, use NetRexx. If you're just looking for a good scripting language and don't know REXX, consider using JPython instead.

NetRexx has a lot of promise, and I look forward to its evolving into an even better and more productive tool. If VisualAge supported NetRexx, I think a lot more people would use it in place of Java - including maybe me.

.   .   .

In the next article we'll cover the Bean Scripting Framework from IBM. The BSF promises to unify integration of scripting languages into applications. It also promises, in the future, to provide universal debugging support for most programming languages for the JVM.

More Stories By Rick Hightower

Rick Hightower serves as chief technology officer for ArcMind Inc. He is coauthor of the popular book Java Tools for Extreme Programming, which covers applying XP to J2EE development, and also recently co-authored Professional Struts. He has been working with J2EE since the very early days and lately has been working mostly with Maven, Spring, JSF and Hibernate. Rick is a big JSF and Spring fan. Rick has taught several workshops and training courses involving the Spring framework as well as worked on several projects consulting, mentoring and developing with the Spring framework. He blogs at http://jroller.com/page/RickHigh.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.