The World Of Java

Overview

Since the first edition of this book in July 1998, the world of Java has continued to gain significant ground. By now, everyone is aware of the industry's commitment to Java, of IBM's commitment to Java, and especially of the AS/400's (now known as the iSeries') commitment to Java. Way back in 1998, the options for AS/400 Java programmers were exactly two: write Java applications that run on the AS/400 or on a client connected to an AS/400, or write Java applets that run in a Web browser connected to an AS/400. Now, in addition to the expanded capabilities of both of these options, there are servlets, JavaServer Pages, and Enterprise JavaBeans (EJBs). Java is now also a full-fledged option for writing Domino applications. Further, XML, with its Java affinity, has emerged as the new standard for business-to-business communication and is a favorite way to persist information needed by tools. A rapidly emerging new standard for reusable components, known as Web Services, also have tight affinity with Java.

Most vendors writing tools have switched to Java. Most package vendors have adopted Java or have a plan to phase it in. Java presentations at professional and technical conferences continue to fill rooms. Look at the agenda for the North American COMMON conference (www.common.org), and you'll see that Java and Java-related technologies permeate it.

So, while Java is still maturing and expanding rapidly, it is now an entrenched part of the iSeries and AS/400 application development scene. At the same time, IBM has proven it is not backing away from RPG or COBOL, and has continued to deliver significant new functionality in each release of both. Further, as of this writing, IBM has just released its new application development package, WebSphere Development Studio for iSeries (WDS), offering modern tooling for everything from RPG to Java to Web user interfaces. That tooling includes WebFacing, an exciting new option for quickly and cost-effectively converting 5250 applications into Web-enabled applications. WebFacing converts display-file DDS source into JavaServer Page source and JavaBeans (simple beans, not EJBs). The WebFacing runtime intercepts the display file input/output operations from an application and diverts them to the WebFacing runtime servlet, offering a Web refacing with no application code change. This awesome new capability is possible due to the power of Java.

The first release of WDS includes the "classic" version of WebSphere Studio for Web user interfaces authored in HTML, JavaServer Pages, and JavaScript, and includes the classic version of VisualAge for Java for applications, applets, servlets, and beans. Also included is the classic version of CODE for RPG, COBOL, C, C++, CL, and lightweight Java development, and even VisualAge RPG for writing Java applications authored in RPG IV. Exciting as this tooling package is on first release, future releases promise to be even more exciting, as all the tools converge around Eclipse, IBM's new Integrated Development Environment that is the basis of the next generation of all IBM tooling. Eclipse is a pluggable and extendable IDE, fully open and indeed open source. What is Eclipse written in, and what is used to extend it? Java, of course. Java is now the cornerstone programming language for IBM.

Evolving the languages and the tools are important enough in their own right, but IBM has also shown it clearly sees a world with both Java and RPG/COBOL by continually enhancing both RPG and COBOL to increase your ability to mix Java and RPG/COBOL code in the same application. V5R1 and later releases of RPG and COBOL bear out this commitment. While Java is clearly the glue of e-Business, IBM will ensure that both RPG and COBOL have roles in this new and exceedingly important picture, for those who prefer to use a business language for their business logic.

With all this in mind, this book will teach you Java so you can include it as another option in your programming toolbox. If you have already read the first edition, welcome back. You will find many additional topics and increased coverage of the core topics. If this is your first read, congratulations on taking the first step on the road to Java.



What is Java?

Java is a programming language. So are RPG and CL. You learned them, and you can learn Java. The reason Java seems like more than just a programming language is the vast number of places it can be used. Vertically, it can be used to program the smallest embedded chips and personal devices all the way up to the largest scalable, transaction-based, distributed enterprise applications. Horizontally, it can run on any operating system and almost any device. Java can be the answer for your user interface, and it can be the language of your next batch language. It can do e-Business on the Internet, and it can do back-office payroll.

However, with all of this, you must distinguish between the core Java language, which is stable, mature, and relatively small, and the Java technologies built on top of that language. These technologies are just specialized Java code written by the industry to solve specific tasks, such as programming chips, Internet Web servers, or large-scale mission-critical applications that support distribution and transactions.

The trick to learning Java is to start with the core language, including the syntax and the core Java-supplied functions. With these under your belt, you can move on to additional Java technologies with relative ease, if and when you need them.

From this book, you will learn the core Java language syntax by having it compared every step of the way to RPG. You will also learn the core Java-supplied functions that you will need to succeed with Java, again compared every step of the way to RPG. With the proper analogies, and the proper definition of terms in iSeries-speak, you'll find that Java is not mysterious and absolutely within your capabilities to learn, use, and master! Indeed, thousands of RPG programmers before you have done exactly that.

This book is not small, but neither are your RPG and DDS manuals. Do not despair, as the size is simply because we have included many examples in both RPG and Java to give you a complete introduction to as much Java as you will need to be productive. This might not be the last book you read on Java, but it should make you immediately productive with Java.



What about That OO Thing?

Java is an object-oriented language. It is inconceivable that a new language today would not be object-oriented; the benefits in productivity and quality are too great to ignore. These gains come from the capacity to reuse code and perform better problem domain-modeling. When people complain about Java's learning curve, they always refer to the OO side of things. This is typically from people who haven't learned Java, or want to promote an alternative. Don't be fooled. Yes, thinking "OO" is different than thinking procedurally as you do in RPG. But it comes quickly, and not by reading a book, frankly. That helps, to be sure, but it really starts coming home only by writing code. With a little experience, you can pick up an OO book or course, and it will all fall into place. Once it does fall into place, you will wonder how you lived without it.

Our approach to OO in this book is not to hit you over the head with it. Our primary goal is to teach you the language and the major functionality supplied with the language. As we do that, we'll be showing you many examples. You will slowly get the hang of objects simply by seeing them, and, if you follow along, by coding them. They really are not that hard. So, don't think OO means "uh oh!" It is just a tool that ultimately will make your life better. (Well, your programming life, anyway.)



Why the Hype?

If Java is just a programming language, then why all the fuss? There are a number of reasons:

The following sections examine these reasons a little further. We hope you like that.

The name

Java was originally designed by engineers from Sun Microsystems. Their intent was to create a small-scale, interpreted language for programming small consumer devices. Reportedly known as "Oak" in its early days, it was originally an embedded language that quickly found a new use for Internet-related programming. It had all the attributes a programming language for the Internet could want: it was small, simple, easy to code, platform independent, and provided support for dynamic loading of code on demand. Thus, it was renamed and made publicly available as a programming language for the Internet in 1995.

Java's big boost, however, came in early 1996, when Netscape Communications stepped up to support it as a programming language that could be processed by its Web browser. Suddenly, thousands of programmers and Web site owners wanted to learn and use Java.

What about the name? Legend has it that it came from the coffee the engineers drank in vast quantities while working so hard on the project. We guess "Donut" was ruled out.

The Internet

The original reason for Java's popularity, then, was the Internet. Specifically, Java could be used to program Web pages displayed by Web browsers such as Netscape's Navigator or Microsoft's Internet Explorer. Web browsers display text and graphics through HTML (Hypertext Markup Language), a tag-based language similar to UIM (User Interface Manager, the AS/400 source language for writing online help).

Java adds the ability to embed Java applets inside HTML pages. Applets are small applications that only run inside Web browsers, using the screen real estate of the browser window. Since they have the full power of a modern programming language behind them, they can provide fully interactive, animated, live-data Web pages, which is beyond what HTML could originally offer. Java can also be used to author servlets, which are server-side programs for serving up Web pages dynamically and on demand.

Java also supports easy Internet communications programming, using either HTTP or TCP/IP communications protocols. Indeed, it is possible to write a Web browser entirely in Java that performs the basic tasks of finding, retrieving, and displaying HTML files. In fact, Sun has done just that with its HotJava Web browser. While this is easy enough, you have no need for communications programming as an AS/400 programmer because IBM supplies the AS/400 Toolbox for Java, a rich collection of pre-written code that makes remotely accessing AS/400 programs and data easy.

Portability

Java approaches more closely than any preceding language that elusive goal of "write once, run anywhere." It is possible to write completely portable applications in Java without too much sacrifice or pain. Here is why this is possible:

Accessibility

Sun Microsystems makes Java available for free over the Internet for Windows and Solaris. IBM and others take that and make it available for their operating systems, Web browsers, computer chips, personal devices, toaster ovens, and so on. In most cases today, Java is actually made part of the operating system, so it is even more accessible.

Visit any Web-search site, do a search for the word Java, and you'll find thousands of free tutorials, newsgroups and other resources at your disposal. Of course, you can pay for onsite education and robust development tools, but one of Java's most alluring features is that you don't have to make that investment. Even your local community college will almost certainly offer a Java programming course. (Finally, the Pascal courses are being retired!)



The Roles of Java

Java can play a role in at least three important places in your application:



The House of Java

Java comes in its purest form as the Java Development Kit, or JDK. This kit contains a number of fundamentals, including these:

These basic elements, together with their documentation, define the Java language. The documentation is in HTML and is shipped compressed into a file that is a separate download from the JDK, so be sure to get it too. While packaged separately, the documentation is still considered part of the JDK. The JDK also includes a number of other useful command-line tools, shown in Table 1.1.

You can find Sun's JDK for Windows and Solaris at www.java.sun.com/products. However, be aware that the official name has changed to Java 2 SDK, Standard Edition, V1.x. So you have to follow the links for Java 2 SDK at Sun's Web site. Further, Java comes with the operating system on the iSeries, but is known there as the Java Application Development Kit (ADK). However, this book uses the simpler term JDK.

Table 1.1: JDK Tools

Tool

Description

javac

Source-to-bytecode compiler

java

Java interpreter

javadoc

Documentation generator

appletviewer

Applet tester

jar

Java archive (compression) tool

jdb

Java debugger (Don't get your hopes up!)

javah

Java-to-C stub generator

javap

Bytecode-to-source de-compiler

extcheck

Jar file conflict detector

rmic

Remote-method-invocation stub generator

rmiregistry

Remote-method-invocation registry

rmid

Remote-method-invocation daemon

serialver

Remote-method-invocation serial-version ID generator

native2ascii

Text-to-Unicode Latin-1 converter

keytool

Security certificate generator

jarsigner

Signature generator

policytool

Security-policy file manager

tnameserv

Naming-service access provider

idltojava

CORBA-IDL-to-Java stub generator

The JDK does not include an editor, an integrated development environment, or a visual design tool, and it provides only the most rudimentary of debuggers. Nonetheless, with a decent editor (such as CODE/400's) you have all you need for basic Java development. Figure 1.1 illustrates the pieces of the JDK.

Figure 1.1: The Java Development Kit (JDK)

Most programmers, however, will opt for one of the many full-scale Java development tools that are already available, such as IBM's VisualAge for Java, Inprise's JBuilder, or Symantec's Visual Cafe. These start with the basic JDK, and then add IDEs (integrated development environments), class hierarchy browsers, debuggers, visual design tools, wizards, and many more items to make the development task easier. Multiple-developer shops will also look for tools that offer built-in team support and versioning control, such as VisualAge for Java offers. (Note that the name "VisualAge for Java" can mean either the classic version of this tool, or its new Eclipse-based generation known as "Java Tools" inside IBM Eclipse-based products such as WebSphere Studio Site Developer and WebSphere Studio Application Developer.)

The JDK originates with Sun Microsystems. Based on tremendous programmer feedback, the company adds enhancements and features to the language, and regularly releases a new version of the JDK. Other industry partners betting heavily on Java work with Sun to define and even implement many specialized pieces.

In February 1997, Sun released version 1.1 of the JDK. In December 1998, it released 1.2, and in February 2000, it released 1.3. Release 1.4 is scheduled for the end of 2001, as of this writing. As of version 1.2, the official generic branding became Java 2 Platform, Standard Edition. Sun's own JDK for Solaris and Sun is named Java 2 SDK, Standard Edition. There is also an Enterprise Edition, which is a superset of the Standard Edition and includes server-side functionality such as servlets, JavaServer Pages, and Enterprise JavaBeans. The JDK is maturing and improving very fast, but hopefully its name has stabilized!

Because this book examines the basics of Java, all its samples are written using Sun's JDK on Windows. We happened to use CODE/400 for entering the source, but you can use whatever tool you have handy. The nice thing about using CODE/400 is that you can use the same editor and debugger for both Java and RPG.

If you decide to follow along with this book's samples, you should get the JDK for Windows from www.java.sun.com/products (follow the link for Java 2 SDK). Also be sure to get the documentation, which is a separate download. When you install the JDK, be sure to follow the instructions in the readme file, especially those related to updating your PATH and CLASSPATH statements on Windows. Other operating-system vendors license the JDK code from Sun and port it as-is to their operating systems. The IBM Hursley laboratory, for example, licenses and ports the JDK to the IBM operating systems AIX, OS/2, OS/400, and MVS, as well as to Linux. For OS/400, the IBM Rochester lab leverages that ported base, and then integrates it deeply into the operating system for improved performance. Various Web browser manufacturers also license the JDK code in order to run Java applets embedded in Web pages.

As part of the Java license, the licensee agrees to stay current with the JDK, and must port a new release within about six months of its release by Sun. This ensures a reasonably current base across all the operating systems and Web browsers, as well as user program portability. The JDK now comes with most, if not all, major operating systems.

It has come with OS/400 since Version 4, Release 2 (V4R2) and has been significantly improved ever since. (For more details, see www.ibm.com/java and www.ibm.com/iSeries/java.)



Java Bytecode

Java achieves its portability because of its bytecode architecture. As illustrated in Figure 1.2, the Java source code you enter is compiled into bytecode using the JDK-sup- plied javac command-line compiler. This compiler is really only the front half of a traditional compiler, in that an executable program is not produced as a result. Rather, it stops at what is referred to as the intermediate code in traditional compilers. This is a more efficient, compact version of the source, which is independent of any particular operating system.

Figure 1.2: Compiling Java source code to Java bytecode

The Java source code is contained in a file that ends with the extension .java ("dot java"), while the Java bytecode is contained in a file that ends with the extension .class ("dot class"). You can copy a bytecode .class file to any operating system or Web browser that contains the Java interpreter, and run it there without change. The hard work is performed by the interpreter itself—it has to be ported. And it is the operating system and Web browser vendors that do this hard work for you.

To run a Java application, you invoke the java command and pass it the name of your bytecode .class file as a parameter, as shown in Figure 1.3. For applets, you need to use something called an APPLET tag in your Web page source. (This tag will be described shortly.)

Figure 1.3: Running a Java application

Sun and other vendors also supply a subset of the JDK called the Java Runtime Environment, or JRE, for the operating systems they support. The JRE consists of only the Java virtual machine (the interpreter) and the supporting runtime packages. As such, it is intended as a distribution option for applications when you do not want to assume the existence of a virtual machine on your users' computers, or if you are worried that their version of the virtual machine might not match yours.



JAVA The Language

Java is based heavily on C++, with some key features borrowed from Smalltalk, Ada, and other languages. The working theme of the Java designers is always KISS ("Keep It Simple, Stupid"). To that end, the language does not have the following:

The biggest plus, from the point of view of programmer productivity, is the absence of memory-management functions. Instead of supplying built-in functions for getting and returning memory, Java offers automatic garbage collection. That is, when you declare variables, Java keeps track of where they are used at runtime, and frees up their memory when it determines your code no longer requires them. If you have written any C or C++ code or used the new memory allocation operation codes in RPG IV (ALLOC, REALLOC, and DEALLOC), you must take care to do the following:

Memory management is very error-prone, and such errors are typically hard to reproduce and difficult to find. As the RPG IV reference manual states, "Misuse of heap storage can cause problems." Indeed. Garbage collection is a language feature Java picked up from Smalltalk, and one that, by itself, saves a significant amount of programmer time. (Now just imagine if someone could manage your real memory! You would save even more time! Maybe in the next release.)

In Java terms, KISS could also be interpreted to mean "Keep It Safe, Stupid" because, in addition to focusing on ease of use, Java focuses on security. An Internet language must be secure; it is not considered polite for code downloaded to client workstations to have the potential for obliterating the data maintained on those trusting, friendly little computers.

Java's extensive built-in security is designed to prevent problems that might arise from both malicious intent and poor programming. The most fundamental of these possibilities is the decision to not define pointers in the language. While this might be seen as a programmer plus (pointer programming, after all, is a battle-won skill), it also stands as a major sand trap for malevolent hackers. Many malicious programs are written by accessing memory that the program is not supposed to utilize, which is done by clever manipulation of pointers. We could tell you more about this but then, of course, we would be creating a potential security problem ourselves. Suffice it to say, "see no pointer, do no evil." (By the way, on the iSeries, pointers are not nearly as dangerous as on other systems because each one encapsulates significant security information. That's why they are 16 bytes long on OS/400, versus 4 bytes on a typical 32-bit operating system.)



The Internet, Html, and Java Applets

Java's initial role in life was that of a Web browser language. In this form, it is used to author applets that run inside Web browsers. Applets resemble traditional applications, except that they are embedded inside HTML and share the Web browser's user-interface real estate.

HTML is a tag-based language that resembles the way UIM (User Interface Manager) is used to author online help pages or menus on OS/400. A tag has both a starting form and an ending form, and the text for that particular tag goes between them. For UIM, the tags are special symbols that start with a colon (:). Typically, the ending tag is the same as the starting tag, but with an e prefix. For example, in UIM, you start a list with :ul (for "unordered list") and end it with :eul (for "end unordered list"). In HTML, the principle is similar, but the syntax is slightly different. Rather than denoting a tag with a colon, HTML encloses it in angle brackets. And rather than ending a tag with an e, it ends a tag with a slash (/). For example, an unordered list is started in HTML by

.

HTML includes many tags for partitioning and formatting text. For example, you can use tags for creating headings, paragraphs, and lists, as well as for formatting text. An example of HTML is shown in Listing 1.1. Figure 1.4 shows how this HTML appears in Netscape's browser.

Listing 1.1: An Example of HTML Code

 

My First Web Page

This is a header


This is a paragraph. Well, you see how simple it is! The following is an ordered list...

  1. Type in the starting tag inside angle brackets
  2. Type in the body between the tags
  3. Type in the ending tag with a slash in front

Figure 1.4: The result of the HTML example in Listing 1.1

HTML tags are not case-sensitive. Some tags, like


(horizontal rule) do not have an ending tag. Others, like

My Second Web Page

To see my first web page, click <a href="html1.htm">here</a>

Категории