Chapter – 3: Vacationing in Java
Before you venture further into Java programming, it's worthwhile to learn more about the language and see what Java programmers are doing today. One of the reasons that Java became popular so quickly was because it could be used to offer programs on the World Wide Web. Though Java has outgrown its origins as a language focused on Web programs, you can still find some interesting examples of how Java is used on the Web.
During this hour, we'll take a look at some sites that feature Java programs and talk about the history and development of the language.
To go on this vacation, you need a Web browser that can handle Java programs. The current versions of Microsoft Internet Explorer, Mozilla, Netscape Navigator, and the Opera browser can run Java programs that are found on Web pages.
Note:
If you're using a current version of Netscape Navigator or Microsoft Internet Explorer and it isn't working with Java programs, check your setup configuration from one of the program's pull-down menus. Make sure your browser software has Java enabled in its settings.
Load your browser software of choice, put on your best batik shirt, and get ready to vacate. Because you won't be leaving your house, you won't get a chance to experience the simpler pleasures of tourism: odd driving rituals, exotic food, exotic members of the opposite sex, exotic members of the opposite sex with food, and so on. But look on the bright side: no antibacterial shots, traveler's checks, or passports are required either.
The following topics will be covered during this hour:
* A definition of the Java language * The benefits of using Java * Some examples of Java at work * An explanation of object-oriented programming * Sites of note for Java programmers
Note:
The sightseeing examples you visit during today's vacation are just a small sampling of the Java programs in use on the Web. A search of the AltaVista Web search database finds more than 7.8 million pages that have included a Java program as of this writing.
First Stop: Sun Microsystems:
The Java vacation begins at a place you'll be visiting regularly, now that you're a Java programmer: the Web site of Sun Microsystems, the company that developed the Java language. To get there, go to http://java.sun.com.A Java program that runs as part of a Web page is called an applet. Applets are placed on pages like other elements of a page—a markup language called HTML is used to define where the program should be displayed, how big it is, and what the program does when it runs.
The Java division of Sun Microsystems is responsible for the advancement of the Java language and the development of related software. As you might expect of a proud parent, Sun uses Java applets on its site. There's a glossary applet that's a guide to the site, several applets that are free for your own use, and others.
The sample applets include an animated clock and a shopping cart assistant. Java can be a great attention-getter, creating content dynamically that changes as a page is being viewed in a Web browser. Figure 3.1 shows the clock and other sample applets.
Figure 3.1 Sun's official Java Web site offers several Java programs for use on your own Web site.
Sun's Java site is the place to find the latest released versions of the Software Development Kit, as well as other programmer's resources. This site also has press releases about Java-related products, full documentation for Java, and sample Java programs that run on the Web. Sun Microsystems first made Java available for free via this Web site in 1995, and it's still the first place to look for each new development kit and addition to the language. A Brief History of Java
Sun co-founder Bill Joy called Java "the end result of fifteen years of work to produce a better, more reliable way to write computer programs." Java's creation was a little more complicated than that.
Java was developed in 1990 by Sun engineer James Gosling as a language to use as the brains for smart appliances (interactive TVs, omniscient ovens, and the like). Gosling was unhappy with the results he was getting by writing programs with C++, another programming language, so he holed up in his office and wrote a new language to better suit his needs.
Today, many of us like writing programs with Java, so we have no incentive to create our own programming languages. As a result, we have more time to hole up in our offices and play EverQuest instead.
At the time, Gosling named his language Oak after a tree he could see from his office window. The language was part of Sun's strategy to make millions when interactive TV became a multimillion-dollar industry. That still hasn't happened today (though TiVo, ReplayTV, and WebTV are making a game attempt), but something completely different took place for Gosling's new language. Just as Sun was ready to scrap Oak development and scatter its workers to other parts of the company, the World Wide Web became popular.
In a fortuitous circumstance, many of the qualities that made Gosling's language good on its appliance project made it suitable for adaptation to the World Wide Web. Sun developers devised a way for programs to be run safely from Web pages and chose a catchy new name to accompany the language's new focus: Java.
You might have heard that Java is an acronym that stands for Just Another Vague Acronym. You also might have heard that it was named for the developers' love of coffee, especially the percolating product from a shop near Sun's offices. Actually, the story behind Java's naming contains no secret messages or declarations of liquid love. Instead, Java was chosen for the same reason that comedian Jerry Seinfeld likes to say the word salsa. It sounds cool.
Although Java can be used for many other things, the Web provided the showcase it needed to capture international attention. A programmer who puts a Java program on a Web page makes it instantly accessible to the entire Web-surfing planet. Because Java was the first technology that could offer this capability, it became the first computer language to receive star treatment in the media. When the language really rose to prominence in 1996, you had to be in solitary confinement or a long-term orbital mission to avoid hearing about Java.
There have been five major releases of the Java language:
* Fall 1995: Java 1.0— A version best suited for use on the World Wide Web that showed potential for expansion into other types of programming * Spring 1997: Java 1.1— An upgrade to the language that included numerous improvements to the way user interfaces are created and handled * Summer/Fall 1998: Java 2, version 1.2— A version more than three times as large as Java 1.0, with enhancements that make the language a worthy competitor to other general-purpose programming languages * Fall 2000: Java 2, version 1.3— A release supporting faster running Java programs and enhanced multimedia features, as well as the first official support for Java development on the Linux operating system * Spring 2002: Java 2, version 1.4— A substantial upgrade with a much-requested feature called assertions to improve software reliability, expanded networking support, and XML processing
Going to School with Java:
As a medium that offers a potential audience of millions, the World Wide Web includes numerous resources for educators and schoolchildren. Because Java programs can offer a more interactive experience than standard Web pages, some programmers have used the language to write learning programs for the Internet.For one of the strongest examples of this use of Java, visit http://www.npac.syr.edu/projects/vishuman/VisibleHuman.html.
This Web site uses data from the National Library of Medicine's Visible Human Project. The project is a database of thousands of cross-sectional images of human anatomy. A Java program is being used to enable users to search the collection and view images. Instead of making requests by text commands, users make the requests to see different parts of the body by using the mouse, and the results are shown immediately in graphic detail. The Java program is shown in Figure 3.2.
Figure 3.2 Images from the National Library of Medicine's Visible Human Project can be viewed interactively on the Web using a Java program.
Numerous educational programs are available for many different computer systems, but what makes this program remarkable is its versatility. The Visible Human Project tool is similar in function and performance to CD-ROM software that users might run on their computer systems. However, it is run directly from a Web page. No special installation is needed, and unlike most CD-ROM software, it isn't limited to PC-compatible and Macintosh systems. Just like Web pages, Java programs can be run on any computer system that can handle them.
To be able to handle Java programs, a Web browser must have a Java interpreter. The interpreter included with a browser serves a similar function as the interpreter you used to run the Saluton program during Hour 2, "Writing Your First Program." The difference is that a browser's interpreter can only run Java programs that are set up to run on Web pages and cannot handle programs set up to run from the command line. Currently, Java-enabled browsers are available for most systems, including PCs running a version of Microsoft Windows, Apple Macintosh systems, SPARC workstations, and computers running the Linux operating system.
The primary Java-capable browsers in use today are Microsoft Internet Explorer, Netscape Navigator, Mozilla, and Opera. Although versions of these browsers support Java, none of them offer built-in support for Java 2. Browser developers have not been able to keep up with new versions of the language as quickly as Sun produces them, and at this time, it appears that all of the companies have given up trying to support anything beyond Java 1.1.
To make it possible for Java programmers to rely on Java 2 support in browsers, Sun has developed the Java Plug-in, a Java interpreter that works as a browser enhancement. By specifying in the coding of Web pages that this interpreter should be used instead of the one built into the browser, Java programmers can take advantage of all features of the language in their Web-based programs.
Note
The Java Plug-in is more than 5 megabytes in size, so it would take more than 45 minutes for a user on a slow Internet connection to download and install the program. However, Sun includes it with the Software Development Kit and other products, so it may already be installed on your computer. If not, you can download and install it as part of the Java Runtime Environment: Visit the Web page http://java.sun.com/getjava/.
A Java program such as the Visible Human Project database does not have to be written for a specific computer system. This advantage is called platform independence. Java was created to work on multiple systems. Originally, Java's developers believed it needed to be multiplatform because it would be used on a variety of appliances and other electronic devices.
The programs you write with Java can be run on a variety of computer systems without requiring any extra work from you. This advantage is one of the primary reasons so many people are learning to write Java programs and are using them on software projects. Many professional software companies are using Java for the same reason. Under the right circumstances, Java can remove the need to create specific versions of a program for different computer systems. The potential audience for software grows with a multiplatform solution such as Java.
Lunch in JavaWorld:
If you didn't lose your appetite after searching through the innards of a visible human, take a lunch break with JavaWorld, an online magazine for Java programmers and other Internet developers. The JavaWorld Web site is available at http://www.javaworld.com.JavaWorld offers how-to articles, news stories related to Java development, and other features in each monthly edition. One of the advantages of the publication's Web format is that it can display functional Java programs in conjunction with articles. Figure 3.3 shows a Java "poetry magnet board" in a tutorial that describes how it is written.
Figure 3.3 A JavaWorld how-to article on how to create a "poetry magnet board" includes a working example of the program.
Note
JavaWorld occasionally moves things around, but at the time of this writing, you can go directly to the poetry magnet board tutorial at http://www.javaworld.com/jw-04-1998/jw-04-step.html. If that page is unavailable, use the site's search engine to look for the word "poetry."
In addition to offering information of benefit to Java programmers, JavaWorld publishes articles and commentary about the language and its development. One issue that has been hotly debated since Java's release is whether the language is secure. Security is important because of the way Java programs work when they are placed on a Web page. The Java programs you have tried during this hour were downloaded to your computer. When the program was finished downloading, it ran on your computer. It was as though someone sat down at your computer, popped in a disk, and ran their own program.
Unless you know a whole lot of people, most of the Web pages you visit will be published by strangers. In terms of security, running their programs isn't a lot different than letting the general public use your computer on alternate weekends. If the Java language did not have safeguards to prevent abuse, its programs could introduce viruses onto your system, delete files, play the collected works of Britney Spears, and do other undesirable things. Java includes several different types of security to make sure that its programs are safe when run from Web pages.
The main security is provided by the following general restrictions on Java programs running over the Web:
* No program can open, read, write, or delete files on the user's system. * No program can run other programs on the user's system. * All windows created by the program will be identified clearly as Java windows. This identification prevents someone from creating a fake window asking for the user's name and password. * Programs cannot make connections to Web sites other than the one from which they came. * All programs will be verified to make sure that nothing was modified after they were compiled.
The general consensus among Java developers is that the language has enough safeguards in place to be usable over the Web. Several security holes have been found, often by programming security experts, and these holes have been dealt with quickly by Sun or the Web browser programmers. Because JavaWorld covers the latest news of note in the Java development community, it is a good way to keep track of any security issues that arise.
Note
None of the safeguards in place are a complete block against malicious programs. Just as loopholes have been found in the past, more will undoubtedly be found in the future. If you are concerned about running Java programs through your Web browser, you can turn off Java support in current versions of Navigator, Internet Explorer, and Opera, though you will be missing a lot of interactive content on the Web.
Version 2 of the Java language introduced a more flexible security policy for programs that run in a browser. You can designate some companies and programmers as "trusted developers," which enables their Java programs to run in your browser without the restrictions that normally would be in place.
This system of trust is established through the use of digital signatures, files that clearly identify the author of a Java program. These signatures are created in collaboration with independent verification groups like VeriSign, which has a World Wide Web site at http://www.verisign.com.
If you have ever authorized an ActiveX control to run in Internet Explorer, you have worked with a similar system of trust and identity verification.
Taking in a Ballgame at ESPN.com:
The first afternoon stop on the Java tour will be a trip to the old ballgame. ESPN.com, one of the leading sports sites on the World Wide Web, is using Java to present baseball games as they happen in a visual, pitch-by-pitch fashion. To see how baseball is played in cyberspace, visit http://espn.com.The Java program called ESPN GameCast presents each pitch in a major league game. Runners are shown on base, player changes are reflected immediately, and all stats in the game are updated in real time. It's a high-tech throwback to the period in the early 20th century when a large outdoor sign would be updated by hand with the score, men on base, and other information about an out-of-town game as it happened.
ESPN's GameCast program is a unique way to follow live games. Figure 3.4 shows GameCast after the last pitch in a thrilling 19-7 win by the Texas Rangers over the Boston Red Sox. (As a Rangers fan, I have to be thrilled about a win, because you never know when it's going to happen again.)
Figure 3.4 The Texas Rangers defeat the Boston Red Sox 19-7 in a game broadcast through ESPN GameCast, a Java program that displays comprehensive information about a game while it is taking place.
One of the things you might notice about ESPN GameCast is that it updates the day's scores from other games as you are using the program. This update is relatively easy to do because the Java language is multithreaded. Multithreading is a way for the computer to do more than one thing at the same time. One part of a program takes care of one task, another part takes care of a different task, and the two parts can pay no attention to each other. Each part of a program in this example is called a thread.
In a program such as ESPN GameCast, the league scoreboard along the top edge of the window could run in its own thread. The rest of the program could be another thread. If you use an operating system such as Microsoft Windows XP, you're using a type of this behavior when you run more than one program at the same time. If you're at work and you surf the Web for European aerobics videos in one window while running a company sales report in another window and making a long-distance call to a friend, congratulate yourself—you're multithreading!
Getting Down to Business:
At this point in your travels, you might be getting the impression that Java is primarily of use to baseball fans and those who have body parts to show the world. Although those two subject areas are enough to keep most of us entertained for days, the next stop on our trip shows an example of Java getting down to business.Direct your Web browser to http://www.uralbeacon.co.uk/zoomchart. This example is a financial charting tool presented as a Java program. Historical price and average data is displayed, and users can drag the mouse over an area on a chart to define a new portion of the data to chart, as shown in Figure 3.5.
Figure 3.5 A Java program from Ural Beacon that is used to analyze financial data such as stock market prices.
Unlike other stock analysis programs that require the installation of software on the computers of each employee who needs access, the use of Java enables Ural Beacon to make the program available to anyone with a Web browser—all the employees would have to do is access the company's Web site.
A program such as Ural Beacon's charting applet can be thought of in several different ways. One way is to think of a program as an object—something that exists in the world, takes up space, and has certain things it can do. Java, like the C++ language, uses object-oriented programming, as you will see during Hour 10, "Creating Your First Object." Object-oriented programming (OOP) is a way to design computer programs. A program is thought of as a group of objects. Each object handles a specific task and knows how to speak to other objects. For example, a word processing program could be set up as the following group of objects:
* A document object, which is the area where you type in text * A spell-checking object, which can look over the document object to find any possible spelling errors * A printer object, which handles the printing of the document * A menu object, a mouse object, and many others
The word processing software is a collection of all the objects necessary to get work done.
OOP is a powerful way to create programs, and it makes the programs you write more useful. Consider word processing software. If the programmer wants to use the spell-checking capabilities of that program with some other software, the spell-checking object is ready for use with the new program. No changes need to be made.
Stopping by Gamelan to Ask Directions:
This world tour of Java programs is being led by a professional who is well-versed in the hazards and highlights of Web-based travel. You'll be venturing out on your own trips soon, so it's worthwhile to stop at one of the best guides currently available for the tourist who wants to see Java, Gamelan at http://softwaredev.earthweb.com/java.Gamelan features one of the most comprehensive directories of Java programs, programming resources, and other information related to the language. Many of the programs visited during this hour were originally found on a trek through the searchable database maintained by Gamelan. Updates are made on a daily basis, so this is another place you'll be visiting often as you develop your Java programming skills.
One of the best uses of Gamelan for programmers is to see what programs are available that offer source code. In case you're unfamiliar with the term, source code is another name for the text files that are used to create computer programs. The Saluton.java file you developed during Hour 2 is an example of source code.
Gamelan's Java Applet Ratings Service (JARS), a directory of Java applets and other resources at the Web address http://www.jars.com, often includes programs that are accompanied by the source code used to create them. After you have finished your first 24 hours as a Java programmer, you ought to take a look at some of these programs. Figure 3.6 shows the ratings service being used to search for "3D graphics" resources.
Figure 3.6 The Java Applet Ratings Service, a part of Gamelan, offers information on hundreds of Java applets and other resources.
The large number of Java programs listed in JARS shows that the language has become adopted quickly by thousands of programmers around the world. Part of the reason is that Java's popularity inspires people to learn it, which is the same principle that caused parachute pants and break dancing to be briefly popular in the mid-80s. Another reason for the swiftly growing population of Java programmers is the simplicity of the language.
One of the goals of Java's design was to make it easier to learn than C++, the language James Gosling was having fits with on Sun's smart-appliance project. Much of Java is based on C++, so programmers who have learned to use that language will find it easier to learn Java. However, some of the elements of C++ that are the hardest to learn—and the hardest to use correctly—have been removed from Java.
For people who are learning programming for the first time, Java might be easier to learn than C++ would be. Also, Java will not work if its variables and other elements of a program are used incorrectly. This adherence to rules can be painful for experienced programmers, but it forces everyone to develop good habits as they create programs.
Some languages are created to make it easier for experienced programmers to harness the capabilities of the computer in their programs. These languages include shortcuts and other features that programming veterans easily understand. Java does not use these features, preferring to make the language as simple as an object-oriented programming language can be. Java was created to be easy to learn, easy to debug, and easy to use.
Because the first experience that most people had with Java was on the Web, it is often thought of strictly as a Web-related programming language. However, Java is not limited to use on the Internet. You can use it to write any kind of software.
Java 2 includes numerous enhancements that make Java a worthy competitor to languages such as Microsoft Visual C++.
One of these enhancements is Swing, a feature that makes it possible to create sophisticated user interfaces for Java programs. These interfaces have a special feature called look-and-feel that enables a Java program to mimic the appearance of different operating systems. Programs can be written to look like Windows software, Motif software, or even Java's own look-and-feel, which is nicknamed Metal.
Sun Microsystems has put its emphasis in recent years on turning Java into a fully capable software development language no matter where the program will run.
Workshop: Putting Java on Your Desktop:
The last stop on your whirlwind tour of Java is the Yahoo! News Ticker, an applet developed for the online news service of the Yahoo! Web site. Put on your fedora and redirect your Web browser to http://www.cadenhead.org/javaticker.The Yahoo! News Ticker is a Java program that provides constant headline updates. The News Ticker is viewed on your system's desktop as a stand-alone window, which you can keep open while your browser is minimized. Figure 3.7 shows the ticker on a Windows XP desktop.
Figure 3.7 News headlines and ads are presented in a desktop window with the Yahoo! News Java Ticker program.
Summary:
Once you're completely caught up on the news events that have taken place during your world-in-a-day jaunt, it's time to put away your luggage and get ready for a return to programming.Q&A:
- Can I use the sample Java programs from the Sun Microsystems Web site on my own home page?
Sun encourages the use of its sample programs on Web sites. In addition to the programs available at http://java.sun.com, take a look at the folders that were created when you installed the Software Development Kit on your system. If you installed the demo programs along with the Kit, you will find more than two dozen sample programs along with the .java files that were used to compile them. These programs can be a valuable resource when you're working on your own Java programs later.
- What is the difference between Java applets and ActiveX controls?
ActiveX, an extension of Microsoft technology called the Component Object Model, offers support for interactive Web programs that are similar in function to Java programs. They are placed on Web pages and run when browsers are equipped to handle them. The primary differences are that ActiveX relies on a system to verify the identity of ActiveX programmers and ActiveX programs are not downloaded each time they are encountered. Unlike Java programs, an ActiveX program stays on a user's system. Also, an ActiveX program is not restricted in what it can do on a system, while Java programs run from Web pages face very strong restrictions in what they can do.
- I ran a useful Java program on a Web page. Can I run it on my system without the browser?
Under most circumstances, no. Java programs typically are developed to run on a Web page or to run from the command line. A program can be written so that it works in both ways, but most of the programs you will find in a directory such as EarthWeb do not include this functionality. You'll learn much more about the different types of Java programs during Hour 4, "Understanding How Java Programs Work."
- Is there a print edition of JavaWorld ?
JavaWorld is distributed strictly through the World Wide Web. However, several newsstand magazines are available that cover the language, including Java Report, Java Developer's Journal, Dr. Dobb's Journal, and others.
- Can a Java program I run on a Web page give my computer a virus?
Because of general security restrictions that prevent Web programs from reading, writing, or modifying files, there's no way for a virus to be transmitted from a Java program on a Web page to your system unless you gave the program full permission to access the hard drive on your system. Java programs you download and run from the command line have the same risk of viruses as any program you download. If you're using programs received over the Internet, you need to acquire a good antivirus program and use it regularly.
Quiz:
If your mind hasn't taken a vacation by this point in the hour, test your knowledge of this chapter with the following questions.Questions
- How did object-oriented programming get its name?
- Programs are considered to be a group of objects working together.
- People often object because it's hard to master.
- Its parents named it.
- Which of the following isn't a part of Java's security?
- Web programs cannot run programs on the user's computer.
- The identity of a program's author is always verified.
- Java windows are labeled as Java windows.
- What is a program's capability to handle more than one task called?
- schizophrenia
- multiculturalism
- multithreading
Answers
- a. It's also abbreviated as OOP.
- b. ActiveX programs verify the author of the program, but this security method is not implemented as a standard security measure of Java. Programmers can use digital signatures and an identity verifying company like VeriSign in Java, but it isn't required.
- c. This also is called multitasking, but the term multithreading is used in conjunction with Java because an independently running part of a program is called a thread.