Most Read This Week
From COBOL or RPG to Java
From COBOL or RPG to Java
Oct. 1, 2002 12:00 AM
This article, tailored for seasoned COBOL and RPG programmers, provides a jump start into the Java programming language and the concepts behind it; it may also be useful for anyone who is stepping into the Java arena for the first time.
Usually depicted as a language suitable only for those with a strong background in C or C++, in reality Java is a modern, eclectic language that builds on a variety of languages, including C and C++, and also borrows many features from Pascal, BASIC, and, yes, COBOL. In the last few years RPG has been moving closer to COBOL, so close in fact that many people say it's just COBOL written on an RPG specification sheet.
In the end, Java is just another language to learn and master. You did it with COBOL and/or RPG, you can do it again.
The difference is that with COBOL and RPG, the path to mastering them appears quite straightforward. You learn structured programming, some instructions, and then make your first attempt with a compiler. The more knowledgeable you become about a specific compiler, the more valuable you are as a programmer.
On the other hand, when facing Java programming, you get tangled in a jungle of unfamiliar terms and concepts: object-oriented programming, threads, sockets, HTML, browsers. And it looks like there's always moreŠand more.
That's just the beginning. In fact, you also need to learn about IP networks, but don't worry, it's not rocket science.
In the Beginning: The Birth of Java
James Gosling and his team came up with a new idea: make a language that's portable and able to run in as many platforms (please read environments) as needed by simply modifying the compiler for the new chipset.
This was not the first attempt to create a portable language. Do you know which one was the first? COBOL. The acronym, as you probably know, stands for COmmon Business-Oriented Language. The key word here is "Common" as it was truly a standardization effort, aimed at the write-once, run-anywhere concept so dear to Java. The C language was the other important try. The two of them failed in the long run. Compilers diverged, and soon there was no portability at all. If you're a COBOL programmer, you surely know what I'm talking about. RPG has better portability, but it's limited to a few IBM proprietary operating systems, so it doesn't count when looking at the industry as a whole.
The Java idea was good; the timing was not. The project failed. A few years later, the Web was in full fashion and Sun had this product ready.
They thought it was a good idea to download a program from the server, along with a Web page and its graphics (if any), and run the downloaded program in the client machine, no matter which hardware/operating system/ browser combination you were using. It was a good idea, but over time the language became more than that.
Let's Stop Talking and Start Coding
Here you can see a couple of interesting things. First, a program in Java is called a class and routines are known as methods. They're almost the same; we'll talk about the differences later. In the code sample, the (one and only) method is called "main". This may remind you of the MAIN SECTION of a COBOL program (mandatory for some compilers). This method has a special "signature" (style of writing) that must be followed exactly if you want your program to be able to run from the command line; that's why you may find the second line a bit strange.
You'll also find that you can use the good old remarks in Java. In the code sample there's a multiline remark at the beginning (in fact, it can have one to n lines) that you may find familiar, as it looks like the mainframe or mini control language remarks, and even quite like the COBOL or RPG remarks. The Java toolkit comes with a tool called Javadoc that can be used to extract the remarks into a group of HTML pages, so they can be used as a basis for documenting an application. Look also at the "//" character group; it marks the beginning of an inline remark (like the ones signaling the end of the method and the end of the class).
Once you've installed it, follow these instructions for a standard Windows PC (it's more or less the same for the other platforms):
1. Open a command (DOS) window.
Note: Remember there are some rules for a Java source file:
1. The file must have the .java extension, so its name should look like
To run a Java class (program) use the following command:
This brings the Java runtime environment to the computer memory, and will in turn load your class (program) and execute it. This is the same thing you do when invoking a COBOL or RPG program from a control language script. After this, your Java class can load other classes and transfer control to them; again, this is quite similar to the concept of a run unit in COBOL or RPG. In Java, this is such a natural thing to do that it doesn't even have a name (i.e., there's no "run unit").
Achieving Portability in Java
You'll find two different packages on the Java Web site: the JDK that I mentioned before, and the Java Runtime Environment (JRE). The latter provides only the runtime, without the programmer's tools, and is useful for deploying applications.
Data and Objects
And there is something good: you can have basic data types as well as complex ones. Maybe you'll think that this is no news, that you actually use complex data types in COBOL by defining groups of data items in the working storage section. Oh, yes, I'm sure you do it, but here you can have executable code perpetually linked with each complex data type that you define.
This is not a minor thing. It brings up a whole new palette of resources and tricks and ways to do things. In fact, this new possibility is so cool it's called object-oriented programming.
An object is just that: a group of fields plus related code, all in a package that can't be (legally) opened. This may not be the best theoretical definition (and maybe it's not even a good one) but I can assure you, from my teaching experience, that it enables you to grasp the concept.
Listing 1 provides a simple program that:
After the method signature you'll find two data definitions. One is a simple, basic variable called a primitive data type in Java. Other object-oriented languages force you to treat absolutely everything as an object; that may be a good thing to do from a theoretical point of view, but in everyday, commercial, production programming practice, it's really a pain in the neck. There's no sense in using an object when all you need to do is count from 1 to 10. (You can use an object if you're a purist, or if you just want to; this is my opinion shared by many people.)
The next one is the definition for using an object. Note that primitive datatype names are in lowercase, and class names must be written with an initial uppercase letter. Java is very case-sensitive, and that's one of the minor things you'll find annoying at first. But remember, COBOL was good because it was very precise and it required a lot of order from the programmer; Java shares the same philosophy, so I guess you'll learn to like it. Using code from other programmers is probably the most useful technique for a professional programmer; in an object-oriented language it's surely the most important technique!
After the definition, the object needs to be initialized. Do so by invoking a special method called a constructor you can conceptually compare it to that frequently used COBOL routine for opening the files; there's no exact example for RPG-loving programmers since we know that RPG opens the files automatically. However, if you're an RPG programmer who knows your compiler, you'll remember that there is one thing called first-cycle processing implicit in the RPG runtime cycle where files get opened.
So, we created a program and it's running in memory. We defined a simple data item and declared an object in memory. The Java environment went to disk, found the class definition, and instantiated an object into memory using that class as a model. I guess this is enough preparation; now let's do our work.
We transfer control from our object (program) to the other object (program) by invoking a method (subroutine) in it. That's what the line c = b.parseInt(args) does. It calls a method called parseInt (short for parse integer) in object b. Object b is equal to the class Integer, so it has all the methods in this class.
I told you there were some differences between a method and a subroutine. It's time to outline them.
Subroutines are like methods in that you can transfer control to both of them. In COBOL or RPG, you can transfer control to any other subroutine in the same program, but if you transfer control to another program, you can't choose the subroutine. It will execute the whole program from the beginning. In Java, you're free. You can execute any method (subroutine) in your class (program) or any method in any other class (if you've previously loaded it into memory; anyway, there is an exception to this).
The other important difference is that you can't pass data to a subroutine; it will look for it in the program's data definitions. But you can pass data to a method; put the data (or a reference to it) between the parentheses following the method name. And a method can also give back data. Let's look at that strange line again:
c = b.parseInt(args)
As you can see, the method receives the first occurrence in the array args[ ] as a parameter. That's the easy part. The one thing you'll find a bit weird is that the method invocation is to the right of an assignment expression (the "=" sign). In COBOL or RPG you must use a variable to do this. Write the subroutine so that it puts the output value in the variable, then move that variable to another. In Java, you can do that in one easy step. The output of the method parseInt is (as its name implies) an integer. And that integer goes to variable c, also an integer.
To end the program we invoke a method called println (meaning print line) in a special system object. This object doesn't need to be instantiated and initialized, because it's a static method. Conceptually this is not very different from writing to the system console in COBOL or RPG; just the code is different.
This is it: Java is easier than it looks at first glance. You can try it at home or at work on a PC; the same code will run on any platform, so you're leveraging your abilities. Java will program almost anything. Once you can code and hack the simple examples, the next best thing is to concentrate on learning one API (set of classes) that you find interesting, understandable, and profitable. Profitability depends on where you live and who your clients are; some will find that AWT (the windowing API) can get them their first real work in Java. I usually teach the servlet API, because it's kind of easy to find some work to do for online applications.
Reader Feedback: Page 1 of 1
Subscribe to the World's Most Powerful Newsletters
Today's Top Reads