Into Java - Part I

From EDM2
Jump to: navigation, search
Into Java / Part
I II III IV V VI VII VIII IX X XI XII
XIII IV XV XVI XVII XVIII XIX XX XXI XXII XXIII

By Simon Gronlund

Summary: If our introductions to Rexx have whet your appitite any, you may now be interested in moving up to another easy-to-learn language: Java.

I doubt anyone has missed the Java wave, but maybe you are still wondering about what Java could do for you. If you'd like to find out, this new regular column will guide you into object oriented developing, try some useful algorithms and show how easy it could be to build graphical interfaces. Java makes all of the above easy, since most of the hard stuff has already been done and either comes with Java or is easy to obtain. In other words, Java gives you what you want, without the headache.

Soon we'll know more about Java inner parts, opportunities and user interaction, as well as basic modeling and design. I won't assume you have any knowledge about any particular programming technique, but I will assume you have some basic coding coding experience, no matter which language. In the future I'll be dividing future columns into a theory lesson and some practical lab work. But today we start with a little of Java's history.

The history lesson

Once upon a time, while working for Sun Microcomputers, a computer scientist named James Gosling gave birth to a new object oriented language that he named Oak. That was eight years back in 1991 when people were still using OS/2 version 1.30 and cheered when Rexx made it into the Standard Edition. Oh, those were the days ...

Oak was primarily developed for use in embedded electronic equipment, such as VCRs, telephones and cable TV boxes. That never surfaced at the time, but it was important to the language because it set the basic characteristic of what we have today in Java. Two of those important qualities are size and reliability.

Embedded systems aren't equipped with huge amounts of memory, therefore the size is important. But even more important is the reliability. Imagine an automatic telephone switchboard running out of memory, or taking a sudden power-failure. You'd quickly learn to appreciate reliability if you lived in that area.

Fortunately, while consumer electronics were still far off, the World Wide Web gave Java a new purpose, since Java really met the needs of the client/server systems inherent to the Internet. The pro's were: the small size suited limited bandwidths, it was an interpreted language with the potential of being supported by any operating system, and it had a security manager. These made Java the perfect tool for applets and soon it was giving many web pages everything from animation to online shopping carts.

What IBM saw in Java is still a mystery, since they already had Smalltalk, a language still widely used in the community of computer science and which still has a number of features Java doesn't have. In spite of that, March 1996 saw IBM gave us a prerelease of Java for OS/2, and six months later we received Warp 4 with Java 1.02. Today, we consider 1.02 useless, but back then it gave us a Java enabled Netscape and a token presence on the Java landscape. However, since then Java has matured from good to better to best on OS/2. If you've forgotten, why not look at Infoworld's review of Java Virtual Machines again and see how OS/2 ranks compared to the others.

Today Java seems to be one of the hottest languages ever, broadly hyped-up by computer media and everyone except Microsoft. Nevertheless, learning Java could never be wrong, it gives you a deep insight in Object Oriented developing and you could always switch to C++ later on (given that the two languages are remarkably similar).

Java basics

As with REXX, Java is an interpreted language. This means that each instruction is translated into native machine code as it runs and your computer will need a Java interpreter to understand how to read each instruction.

But you ask, "Don't you compile the Java code?" Yes, you do, but you're not compiling to machine code as you would with a traditional compiled language. A computer understands machine code as you understand your native tongue, fast, fluently and faultless. Machine code speaks right to the computers heart, it's CPU. Only the operating system lays in between to control the actions, supporting the app with memory, creating windows on demand, and other basic tasks. But Java is compiled to a device-independent, operating system-independent, bytecode.

Bytecode (so-called because it is mostly made up by combinations of instructions of one or two bytes long) is kind of a pseudo-machine-language, understood by an interpreter, a Java Virtual Machine. The JVM reads the Java bytecode and executes it, byte by byte. The JVM is, on the contrary, 100% dependent upon the actual operating system.

Compared to C this takes time and so Java has been belittled as sluggish. Alas, if you're planning to write yourself a new database managing the telephone numbers of New York city, you'd better pick another language. But, you could easily use Java to build your user-interface to this huge database as well as every module around it.

Further, Java can either be used to create a stand-alone application, or browser-dependent applets. The main difference is that you have to embed an applet in a HTML web page and run it in another framework, like your Netscape browser, or the OS/2 appletviewer. An application has no need of a framework, except the Java Virtual Machine that's installed on your computer. There are differences between apps and applets, but we'll not bother with these for the moment.

Your Java developing environment

If you haven't already set up an environment for developing Java, there's only a few short steps you need to create one.

First. See to it you have at least one HPFS partition. When Java was developed, the designers thought it was about time to finally give up the old 8.3 filename limit, meaning that it's almost impossible to do any Java development on FAT formatted drives.

OS/2 - Window
[C:\]md OS2-eZine_Java
[C:\]cd OS2-eZine_Java
[C:\OS2-eZine_Java]md HelloApp
[C:\OS2-eZine_Java]cd HelloApp
[C:\OS2-eZine_Java\HelloApp]
Figure 1. Make yourself some new folders to use.

Second. Make yourself a new folder to use for your work (Figure 1). We'll use several examples and a lot of the stuff will be reused, as it should in object oriented developing.

Although there are a number of visual developing tools available, you are in no need of them now. They serve their purpose, but I want you to get the basic feeling of Java inner parts. Frankly, you got the first tool already, E.EXE, which comes with OS/2. Or you could use EPM, or any tool you find yourself comfortable with. The advantages with developing tools are, you'll get colored key words, automatic indentation and other nifty features.

Third. Go get the software needed. To install Java you are in need of a few items. Follow these steps:

  • Get up an early Saturday morning, when every other web surfer is asleep. Bring your biggest coffee thermos, a virtual shopping bag and plan for a long download (unless you've got a T1 line). Point your browser to OS/2 Software Choice and choose your language.
  • If you haven't got the freshest version of Netscape, get it now, you'll need it later on.
  • Put "OS/2 Feature Install Version 1.2.3" in your bag. (And don't ask me why IBM want us to do it this way).
  • Then grab Java 1.1.7. Get the "Java Runtime Package" (and if International languages aren't your priority, get the one without the Unicode font, since it's 12 megabytes more), and the "Developer Package".
  • After you've downloaded that, and shattered your stomach with way too much coffee, you have to install the stuff. Do it in the same order you downloaded the files:
    1. If you had to get Netscape Communicator, execute the self extracting file in a temporary folder and follow the instructions (mainly, run install, then reboot).
    2. Now for Feature Install, which you unzip it into another temporary folder and then run FISETUP.EXE. This adds a plugin to Netscape for use by the HTML-based Java install program.
    3. Now unzip the two Java files into one temporary folder, and then run INSTALL.EXE. Follow the instructions you'll get. After install is finished, shut down your computer and reboot. At last, delete the contents of the temporary directories.
  • Now point your browser to ftp://ftp.hursley.ibm.com/pub/java/fixes/os2/11/117/ to get the latest fixes for Java. Three files. Yes, they are big, but they are worth it. As of writing this, the latest version is "JDK 1.1.7A IBM build o117-19990218".
  • Copy these files to the root on the drive where you have the JAVA11 folder, not in that folder but in the root, such as C:\. Execute them, starting with RUNTIME.EXE, then TOOLKIT.EXE and SAMPLES.EXE. These are self-extracting files that had their directory structures kept intact, making them unpack their files to where you should have installed Java.

Say Hello World

Java1.gif

We will conclude this first part of our column with the most oft written application ever: "Hello World." Open up an OS/2 window alongside your text or programming editor of choice (in our case, MED), then follow me.

Our "Hello World" example is shown here in this screenshot, (Figure 2) that's deliberately replacing normal text so you can't cheat by using cut and paste ;-) Remember that Java is sensitive to the type of parentheses or bracket you use, with there being three kinds: { } [ ] ( ). They are always in pairs.

Be sure you save the file with exactly this name in your HelloWorld folder that you made before: HelloWorld.java. Java is very sensitive with upper and lower case names, including file names.

OS/2 - Window
[X:\]
[X:\]javac HelloWorld.java
[X:\]java HelloWorld
Hello World!

[X:\]
Figure 3. Compile and run your first Java app.

After you've written the code and saved it, you're off to execute the built in Java compiler. And after that we will test the OS/2 JVM (Figure 3).

The first command line javac HelloWorld.java starts the Java compiler. The result will be a new file called HelloWorld.class.

The second command line java HelloWorld is actually starting the OS/2 JVM, executing the file HelloWorld.class. The extension 'class' is always omitted.

Now you could go tell your friends you've spent this time making a Java application ;-)

Did I hear someone mention "useful"? Ahem, could we save some of the goodies to the future? So you will be back. See you next month!