Appendix . Bonus Article: Choosing Java

Sams Teach Yourself Java 6 in 21 Days covers a cross section of Java’s most popular capabilities, including Internet networking, file input and output, graphical user interface design, event handling, Extensible Markup Language (XML) processing, Java servlet programming, and database connectivity.

The programs you write with Java can run on a diverse number of computing environments, such as desktop systems, web browsers, Internet servers, home appliances, and cellular phones

In this article, you’ll learn why more than 4 million other programmers have used the language. If you’re unfamiliar with the origins of Java, or if you’re still unsure about whether it will be suitable for the kind of software development you undertake, this primer addresses the following questions:

  • Where Java is today and how it got there

  • Why Java is worth learning

  • Why Java is being chosen for software projects

  • What you need to start writing Java programs

Java’s Past, Present, and Future

Based on the enormous amount of press Java has received over the years and the huge number of books about the language, you might have an inflated impression of Java’s capabilities.

Java is a programming language well suited to designing software that works in conjunction with the Internet. It’s also an object-oriented programming language, using a methodology that is extremely successful in the world of software design. Additionally, it’s a cross-platform language, which means that its programs are designed to run without modification on Microsoft Windows, Apple Macintosh, Linux, Solaris, and other systems. Java extends beyond desktops to run on devices such as televisions and phones.

Java is closer to programming languages such as C#, C++, and Python than it is to a page-description language such as Hypertext Markup Language (HTML), a web scripting language such as JavaScript, or a data description language such as XML.

Interactive Web Programming

Java first became popular because of its capability to run on web pages. Using a plug-in, Microsoft Internet Explorer, Mozilla Firefox, and other browsers can download a Java program included on a web page and run it locally on the web user’s system.

These programs, which are called applets, run interactively on web pages—taking user input, responding to it, and presenting ever-changing content.

Applets can be used to create animation, charts, games, multimedia presentations, and other interactive effects.

Figure 1 shows an applet running in its own window using Java Web Start, a technology for launching software from web pages. This applet, Every Icon, is an interactive work of art implemented in Java by John F. Simon, Jr., an artist and programmer who has taught at the School of Visual Arts in Manhattan. It has been shown at the Whitney Biennial art exhibition and purchased by the Guggenheim Museum and the San Francisco Museum of Modern Art.

A Java applet that displays billions of black-and-white icons in sequence.

Figure 1. A Java applet that displays billions of black-and-white icons in sequence.

Note

The Every Icon applet is designed to display every possible icon that can be drawn using black or white squares in a simple 32-by-32 grid. Though the applet displays icons quickly, it takes more than 16 months on a Pentium-equipped computer to display all 4.29 billion variations possible on the top line of the grid alone. Displaying all variations on the top two lines would take around 16 billion years. Displaying all icons on all 32 rows would take more years than there are atoms in the universe. You can find Every Icon and Simon’s other art projects by visiting http://www.numeral.com.

Applets are downloaded over the Web just like pages, graphics, and any other element of a website. On a web browser equipped to handle Java, the applet begins running when it finishes downloading.

Applets are written with the Java language, compiled into a form that can be run as a program, and placed on a web server.

The programs are run by the Java Plug-in, a browser enhancement offered by Sun Microsystems. If a user visits a page containing an applet with a browser that cannot run Java, a dialog box opens, asking whether the Java Plug-in should be downloaded and installed.

Note

You can learn more about applets in “Writing Java Applets,” a bonus chapter on this book’s CD.

Applets have grown far less common on the Web, where a rival technology called Macromedia Flash has become the most popular means of presenting interactive content.

But the language has continued to rise in popularity because it can be used to develop a wide range of software supporting graphical user interfaces, networking, database connectivity, and other sophisticated functionality.

Java programs that don’t run within a web browser are called applications.

Java Grew from a Little Oak

The Java language was developed at Sun Microsystems in 1990 as part of a project codenamed Green that was a research effort into consumer electronics. Sun was working on a programming language for smart appliances to talk to each other, so your refrigerator could tell your phone to call you when the milk’s gone bad.

To put its research into action, Green developed a prototype device called the Star 7, a handheld gadget resembling today’s PDA devices that could communicate with others of its own kind.

The original idea was to develop the Star 7 operating system in C++, the object-oriented programming language created by Bjarne Stroustrup. When Green project member James Gosling became fed up with how C++ was performing on the task, he barricaded himself in his office and wrote a new language to handle the Star 7 better.

The language was named Oak in honor of a tree Gosling could see out his office window. It was later renamed Java in honor of the lawyers who found out about another product called Oak (and didn’t want Sun to go out on a limb).

Because Java was designed for embedded electronic devices instead of state-of-the-art PCs, it had to be small, efficient, and easily portable to a wide range of hardware devices. It also had to be reliable. People have learned to live with occasional system crashes and lockups in a 30MB software application. However, there aren’t many people willing to debug an elevator while its programmers work out the kinks.

Java wasn’t catching on as an appliance development tool, but just as things were looking grim for the Green project, the World Wide Web started to take off. Many things that made Java good for the Star 7 turned out to be good for the Web:

  • Java was small—Programs loaded quickly on a web page.

  • Java was secure—Safeguards prevented programs from causing damage, whether accidental or intentional.

  • Java was portable—Owners of Windows, Macintosh, Linux, and other operating systems could run the same program in their web browsers without modification.

To demonstrate Java’s potential, in 1994, project members created HotJava, a web browser that could run Java applets. The browser demonstrated two things about Java: what it offered the Web and what kind of program Java could create. Green programmers had used their new language to create the browser instead of implementing it in C++.

Netscape became the first company to license the Java language in August 1995, incorporating a Java interpreter in its then-leading Navigator web browser. Microsoft followed by licensing Java for Internet Explorer. Millions of people could run interactive programs in their browsers for the first time.

Spurred by this huge audience of web users, thousands of people learned Java programming and Sun added hundreds of employees to its Java effort, believing that the language was ideally suited for a wide variety of computing platforms beyond the Web.

Versions of the Language

Sun has released seven versions of the Java language:

  • Java 1.0, a small web-centered version available in all popular web browsers

  • Java 1.1, a 1997 release with improvements to the user interface and event handling and a new component technology called JavaBeans

  • Java 2 version 1.2, an expanded version released in 1998 with better graphical user interface features and database connectivity

  • Java 2 version 1.3, a 2000 release that added multimedia and accessibility features

  • Java 2 version 1.4, unleashed in 2002 with XML processing, assertions, regular expressions, and improved networking

  • Java 2 version 1.5, a 2004 release with generics support, more for loops, and automatic conversion between objects and primitive types

  • Java 6, which introduces XML web services, a built-in JDBC database and scripting language integration

The features described in the preceding list are part of the Java Standard Edition, the most popular edition of the language, which has a target audience of software developers working in a variety of corporate, academic, and governmental environments.

There are also two other editions:

  • The Java Enterprise Edition, an expanded edition for the developers of web applications and enterprise systems—large-scale, computing-intensive projects with increased requirements for scalability, portability, and the need to work in conjunction with older hardware and software

  • The Java Micro Edition, a smaller edition designed to run on personal digital assistants, cellular phones, and other consumer devices—returning Java to its roots

This book covers the Standard Edition, which is the best mix of features for the largest audience of Java developers and the people they write software for.

It gives special emphasis to the Java Development Kit (JDK), a free development tool available from Sun’s Java website at http://java.sun.com.

The JDK, which is free, is the first development tool that supports new versions of Java.

In addition to the JDK, more than two dozen commercial development tools are available for Java programmers. The following are some of the most popular:

  • Borland JBuilder

  • Oracle JDeveloper

  • Sun NetBeans

  • IntelliJ IDEA

  • Eclipse

If you are going to use something other than the JDK to create Java programs as you read this book, you need to make sure that your development tool is up to date in its support for Java 6. Many of these tools can be configured to work automatically with new JDK releases.

Java’s Outlook

Anyone who can accurately predict the future of Java should be going after venture capital instead of writing a book. The technology firm Kleiner, Perkins, Caufield, and Byers (KPCB) created a fund to invest $100 million in startup companies on the basis of their future plans involving Java, distributing millions to Marimba, Resonate, and a dozen other companies.

With the caveat that neither author of this book is pursuing venture capital, we predict a bright future for Java over the coming decade.

The new version of Java incorporates the following key improvements:

  • XML web services—A way to write Java programs that exchange data with XMLRPC and SOAP web services

  • Built-in database—Convenient relational database access over JDBC with Java DB, a database included with the JDK

  • Scripting integration—The ability to use Java programs that work in concert with scripting languages

You work with these and other new features in this book.

Why Choose Java

Java applets were a breakthrough in interactive content on the Web, adopted by sites to deliver news and present information.

Applets are still on the Web today, but the most exciting and useful Java-related developments are occurring elsewhere.

A great example of this is Jini, Sun’s Java-based technology for connecting computers and other devices together. The goal of Jini is effortless networking—connect two Jini devices together, and they instantly form a network without the need to run any special installation or configuration programs.

Jini, which ironically returns Java to the original goals of the Green project, is just one of the new areas where the language is being employed.

Another is support for Java programs that run on Internet servers, either as web applications called servlets or as JavaServer Pages—web documents that combine Java code with HTML markup tags and content. Java’s server technology is being used on thousands of websites to generate dynamic content in response to user input.

Regardless of where you find it running, Java’s strengths remain its object-oriented nature, ease of learning, and platform neutrality.

Java Is Object-Oriented

If you’re not yet familiar with object-oriented programming, you get plenty of chances to become so during the first week of this book.

Object-oriented programming (OOP) is a way of conceptualizing a computer program as a set of separate objects that interact with each other. An object contains both information and the means of accessing and changing that information—an efficient way to create computer programs that can be improved easily and used later for other tasks.

Java inherits many of its object-oriented concepts from C++ and borrows concepts from other object-oriented languages as well.

Java Is Easy to Learn

In part, Java was first created at the Green project in rejection of the complexity of C++. C++ is a language with numerous features that are powerful but easy to employ incorrectly.

Java was intended to be easier to write, compile, debug, and learn than other objectoriented languages. It was modeled strongly after C++ and takes much of its syntax from that language.

Note

The similarity to C++ is so strong that many Java books make frequent comparisons between the features of the two languages. Today, it’s more common for a Java programmer to learn the language before or in place of C++. For this reason, there aren’t many references to C++ in this book.

Despite Java’s similarities to C++, the most complex and error-prone aspects of that language have been excluded from Java. You won’t find pointers or pointer arithmetic because those features are easy to use incorrectly in a program and even more difficult to fix. Strings and arrays are objects in Java, as is everything else except a few primitive data structures such as integers, floating-point numbers, and characters.

Memory management, also called garbage collection, is handled automatically by Java rather than requiring programmers to allocate and deallocate memory. Perhaps the biggest difference between the languages is that multiple inheritance is not supported in Java.

Experienced C++ programmers will undoubtedly miss these features as they start to use Java, but everyone else will learn Java more quickly because of their absence and write more reliable programs.

Although Java is easier to learn than many other languages, a person with no programming experience at all will find Java challenging. It is more complicated than working in something such as HTML or JavaScript, but definitely something a beginner can accomplish.

Note

Sams Publishing publishes another Java tutorial aimed directly at beginning programmers: Sams Teach Yourself Programming with Java in 24 Hours, Fourth Edition, by Rogers Cadenhead, one of the coauthors of this book.

Java Is Platform Neutral

Because it was created to run on a wide variety of devices and computing platforms, Java was designed to be platform neutral, working the same no matter where it runs.

This was a huge departure in 1995, when Visual C++, Visual Basic, and other leading programming environments were designed almost exclusively to support versions of Microsoft Windows.

The original goal for Java programs to run without modification on all systems has not been realized. Java developers routinely test their programs on each environment they expect it to be run on, and sometimes they are forced into cumbersome workarounds as a result. Even different versions of the same web browser can require this kind of testing. Java game programmer Karl Hörnell calls it a “hopeless situation.”

However, Java’s platform-neutral design still makes it much easier to employ Java programs in a diverse range of different computing situations.

As with all high-level programming languages, Java programs are originally written as source code, a set of programming statements entered into a text editor and saved as a file.

When you compile a program written in most programming languages, the compiler translates your source file into machine code—instructions specific to the processor your computer is running. If you compile your code on a Windows system, the resulting program will run on other Windows systems but not on Macs, Palm Pilots, and other machines. If you want to use the same program on another platform, you must transfer your source code to the new platform and recompile it to produce machine code specific to that system. In many cases, changes to the source are required before it compiles on the new machine because of differences in its processors or operating system functionality and other factors.

Java programs, on the other hand, are compiled into machine code for a virtual machine—a program that acts as a sort of computer within a computer. This machine code is called bytecode, and the virtual machine takes this code as input and carries out each instruction.

The virtual machine is commonly known as the Java interpreter, and every environment that supports Java must have an interpreter tailored to its own operating system and processor.

Java is also platform neutral at the source level. Java programs are saved as text files before they are compiled, and these files can be created on any platform that supports Java. For example, you could write and compile a Java program on a Windows XP machine, upload it to a Linux machine over the Internet, and run the compiled version.

Java interpreters can be found in several places. For applets, the interpreter is either built into a Java-enabled browser or installed separately as a browser plug-in.

If you’re used to the way other languages create platform-specific code, you might think the Java interpreter adds an unnecessary layer between your source file and the compiled machine code.

The interpreter does cause some performance issues—as a rule, Java bytecode executes more slowly than platform-specific machine code produced by a compiled language, but each version of the language brings better speed, as does each new generation of processors.

For some Java programs, speed might not be as much of an issue as portability and ease of development. The widespread deployment of Java in large business and government projects shows that the loss in speed is less of an issue than it was for early versions of the language.

Java is a different language today than it was in 1995.

This has a good side—there’s a well-established market for Java programmers, and the skills are in strong demand.

This also has a bad side—Java is 20 times larger today than it was on its first release, so there’s much more to learn.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset