Java is a relatively new language on the iSeries. Many people talk about it, but few i5 veterans know much about Java. This chapter is devoted to "demystifying" Java. It covers the very bare bones basics of the Java language, not enough to enable you to crank out Java applications. The complexities involved in developing and deploying full Java applications are beyond the scope of what we can do in this chapter. Instead, we will focus on introducing the basics so that, when you do take on the challenge of developing your first Java application, the foundation of the language will already be set, and you can focus on the application rather than the syntax.
Java is a powerful new programming language resembling C++. In 1995, James Gosling of Sun Microsystems worked on a project to develop a language for programming smart home appliances. While doing so, he became frustrated with the C++ language and he decided to come up with something better. He removed all the things he did not like from C++, and the result was Java. Even though the project failed, Java found a home in programming for web browsers. It received a lot of media attention and its popularity quickly grew. What makes Java so popular? What gives Java its great potential?
Java is platform-neutral.
Java is object-oriented.
Java has no pointers.
Java is simple (compared to C++).
Java has automatic garbage collection.
The Java compiler does not create machine-executable code. Rather, it creates Java Class objects (programs) containing Java Byte Code. This symbolic form represents a generic task with no specific instructions for performing that task on any particular computer system. Each operating system that supports Java has its own Java Virtual Machine (JVM). As illustrated in Figure 26.1, the JVM interprets the byte code of each Java class object and translates that into the specific instructions needed on that particular computer system. This makes Java platform-independent and allows developers to use Java to create applications that run on virtually any platform.
Figure 26.1: JVM architecture.
Procedural languages such as RPG and COBOL make up the majority of the traditional applications on the i5 platform. These languages focus on providing instructions to the computer. Object-oriented languages, such as Java, focus on creating structures that reflect the real-world behavior of whatever they model.
Modeling uses the class objects mentioned earlier. Class objects represent a single real-world object. This could be anything: a person, a document, or even a vehicle. Figure 26.2 demonstrates that each class has properties that describe its attributes and methods that describe its actions. In addition, each class can inherit the properties and methods of another class.
Figure 26.2: Object orientation and inheritance.
One of the key differences between Java and C++, its predecessor, is that Java does not allow direct manipulation of memory pointers. Allowing programmers to work directly with pointers is both a powerful and dangerous technique. Java avoids pointers to make it safer and easier to use.
Another complex aspect of C++ that Java omits is polymorphism. In C++, polymorphism allows objects to inherit properties and methods from multiple parent objects. Managing this and resolving conflicts can be a challenge, so Java uses a simpler single inheritance model. Each class object may inherit properties and methods from only one parent class.
Memory management is challenge for most programmers. Applications often have "memory leaks," meaning that memory was allocated for some task and not released when the task was complete. Over time, this slows down the computer and eventually forces a reboot. Java assists programs deal with the unneeded objects through a process called garbage collection. Periodically, the JVM will identify unneeded classes and clean them up.