Welcome to Teach Yourself VJ++ in 21 Days! For the next three weeks you'll learn all about the Java language, Java applets, and Java applications. Not only will you learn these important and transportable fundamentals, but also how to use
Microsoft's Visual J++ development tool to quickly and efficiently create reusable Java programs using the standard Java language. You'll also get a taste of how to extend your Java programs using the Microsoft Component Object Model (COM) platform
including ActiveX.
As you might know, all of the preceding browsers listed are World Wide Web browsers used to view Web pages, follow links, and submit forms. They are also capable of downloading and playing Java applets on the user's system.
Today you'll learn about the following:
Java is an object-oriented programming language developed by Sun Microsystems, a company best known for its high-end UNIX workstations. Modeled after C++, the Java language was designed to be small, simple, and portable across platforms and operating
systems, both at the source and at the binary level (more about this throughout book).
What makes Java different from most other programming languages is that, in addition to being a full featured language capable of creating stand-alone applications, it can create Java applets. Java applets have the same basic functions as Java
applications, but their raison d'être is the ability to run inside a Java-capable browser. Applets appear in a Web page much in the same way as images do, but unlike images, applets are dynamic and interactive. Applets can be used to create
animation, figures, or areas that can respond to input from the user, games, or other interactive effects on the same Web pages among the text and graphics. As a Java developer you need only create the applet once; the Java-capable browsers perform the
interpretation specific to their own platform.
Although HotJava (Sun's browser) was the first World Wide Web browser to be able to play Java applets, Java support is available in other browsers. Microsoft Internet Explorer 3.0 and Netscape version 2.0x and greater provide support for Java applets.
Other browser developers have also announced support for Java in forthcoming products.
To create an applet, you write it in the Java language, compile it using a Java compiler, and refer to that applet in your HTML Web pages. You put the resulting HTML and Java files on a Web site much in the same way that you make ordinary HTML and
image files available. Then, when someone using a Java-capable browser views your page with the embedded applet, that browser downloads the applet to the local system and executes it. The user can then view and interact with your applet in all its glory
(users using non-Javaenabled browsers might see text, a static graphic, or nothing). You'll learn more about how applets, browsers, and the World Wide Web work together further on in this book.
The important thing to understand about Java is that you can do so much more with it besides create applets. Java was written as a full-fledged programming language in which you can accomplish the same sorts of tasks and solve the same sorts of
problems that you can in other programming languages, such as C or C++. To give you an idea of the power of Java, HotJava itself, including all the networking, display, and user interface elements, was written in Java.
Throughout the remainder of this book, Visual J++ will be referred to as VJ++.
VJ++ is Microsoft's Integrated Development Environment (IDE) for developing, compiling, testing, and debugging your Java applets and applications. It's based upon Microsoft Developer Studio (See Figure 1.1.) If you've used Visual C++ or Visual Test,
you'll be quite familiar with the workings of the VJ++ IDE. For those of you who haven't used Microsoft Developer Studio, VJ++ delivers what you would expect from a Java development tool, and, additionally, has robust features for ease-of-use, re-use of
common libraries, and maintenance of your Java code. VJ++ also has the built-in capability to combine Microsoft's new technology, Component Object Model (COM), with your Java applets and applications. You'll learn all about the Microsoft Developer Studio
starting tomorrow.
Figure 1.1. Microsoft Developer Studio.
Up to this point, we've discussed Java, Java applets, Java applications, development tools, and Web browsers. To put it all in a basic context, Listing 1.1 is a simple example demonstrating how your Java applets are viewed by the world. The first
step-by-step hands-on exercise you'll do in this book will be to re-create this example.
To include an applet in a Web page, you refer to that applet in the HTML code for that Web page. Here is a very simple HTML file (See Listing 1.1.) Notice the line beginning with <APPLET CODE=. This line in the HTML file causes a Java-capable Web
browser to load and play the applet contained in the quotes ("HelloWorldApplet.class"). Notice also, the .class file extension, which all compiled Java applets have.
Listing 1.1. The HTML with the applet in it.
1: <HTML> 2: <HEAD> 3: <TITLE>Hello to Everyone!</TITLE> 4: </HEAD><BODY> 5: <P>My Java applet says: </P> 6: <APPLET CODE="HelloWorldApplet.class" WIDTH=150 HEIGHT=25><APPLET> 7: </BODY> 8: </HTML>
You refer to an applet in your HTML files with the <APPLET> tag. You'll learn more about the <APPLET> tag later on, but here are three things to note:
Once an applet has been included as a reference in an HTML page, all you need is a Java-enabled Web browser to view it. If you don't have a proper browser, the appletviewer application, which is part of the Java Development Kit, will work. Although the
appletviewer is not a Web browser and won't enable you to see the entire Web page, it's an acceptable alternative to test how an applet will look and behave.
If you're using HotJava to view your applets, make sure it's not the alpha version. Applets developed with VJ++ cannot be viewed with the alpha version.
Because you'll probably be using Microsoft Internet Explorer 3.0 to view an applet file (see Figure 1.2), you can use File | Open and type in the name of the HTML file, or use Browse to navigate to the HTML file containing the applet.
Also, because you're viewing a local copy of the applet file, you don't need to connect to a Web site.
Figure 1.2. Microsoft Internet Explorer 3.0.
Depending upon your point of view, creating applets that are available to everyone who has a Java-capable browser might be the most important thing that Java can do. Or, the fact that Java is a full-featured, object-oriented development language,
capable of producing the same kind of applications as C++ could be the reason you want to learn Java development.
Java programs fall into two main groups: applets and applications.
Applets, as you know, are Java programs that are downloaded over the World Wide Web and executed by a Web browser on the user's machine. This makes applets dependent upon a Java-capable browser. Applets do not have the necessary code to run
independently of a browser. Applets are therefore smaller than applications because each browser, not applet, contains the Java virtual machine to do all the work to run the applet.
Java applications are more general use programs written in the Java language. Java applications don't require a browser to run, and in fact, Java can be used to create the same applications that you would normally use another programming language to
create. As mentioned before, Sun's HotJava browser is a Java application. It, as well as other Java applications, have all the necessary code to run independently of a browser; they need only the Java virtual machine to run.
Other than the preceding basic browser or non-browser distinction, there is really very little difference between an applet and an application. A single Java program can be an applet, an application, or both, depending upon how you write that program
and the capabilities that the program uses. Throughout this book you'll be creating both applets and applications. But wait, before we go on about the exciting development projects you'll be involved in, a little bit of history.
The Java language was developed at Sun Microsystems in 1991 as part of a research project to develop software for consumer electronics devicestelevision sets, VCRs, toasters, and the other sorts of machines you can buy at any department store.
Java's goal at that time was to be small, fast, efficient, and easily portable to a wide range of hardware devices. It is that same goal that made Java a general purpose programming language for developing programs that are easily usable and portable
across different platforms, as well as an ideal language for distributing executable programs via the World Wide Web.
The Java language was used in several projects within Sun, but did not get very much commercial attention until it was paired with HotJava. HotJava was written in 1994 in a matter of months, both as a vehicle for downloading and running applets and
also as an example of the sort of complex application that can be written in Java.
Shortly thereafter, Sun released to the public the Java Developer's Kit (JDK). Version 1.0 of the JDK includes tools for developing Java applets and applications on Sun systems running Solaris 2.3 or greater and for Windows NT, Windows 95, and the
Macintosh. However, the JDK is a command line based Java development environment. And, as the word kit impliesyou have to put the separate tools together yourself, making it not the easiest tool to use for development efforts.
All the examples and code in this book are written to support the Java 1.0 language specification released with the JDK.
Today, Java has taken the world by storm. There are a flood of tutorial and reference books on the market. The latest Netscape and Microsoft browsers are Java-enabled. New Java-specific magazines and conferences abound. Each time you revisit a Web
page, more things dance, jump, sing, and want to interact with you. Web pages that are not Java-enabled are sliding off the Hot List. In short, if you ask a group of people "What's Java," very few will say it's coffee, and even fewer still will
say, "It's a densely populated Indonesian island republic whose chief agricultural crop is actually rice and not coffee."
What's in store for Java's future? Sun is still committed to Java. They are preparing a new release of the JDK, and they've also released Java Workshop, a Java development tool using a GUI environment to facilitate the creation of Java applets. Other
major companies like Microsoft, IBM, Novell, Netscape, Borland, and Symantec have licensed the Java technology from Sun. This indicates that these industry leaders have confidence in the viability of Java as a serious development language. From some of
these companies, end-user tools incorporating Java-enabled technology are available, for example, the Microsoft Internet Explorer and Netscape browsers. Development tools such as Microsoft's VJ++, and Symantec's Café have been released. Borland has
been working on their Java-based visual development tool, Latte, which is scheduled for release later this year. Java platform extensions such as COM from Microsoft as well as application development tool extensions for Microsoft's VC++ and Borland's C++
5.0, are also available.
As of this writing, both Microsoft and Netscape are developing just-in-time compilers for Java applets and incorporating support for the compilers into their browser products. These compilers will decrease the amount of time it requires for an applet
to run in a Web browser. The browser and compiler work in conjunction to download, compile, and display the applet. Microsoft's plans for Java go even further. By the end of the year, Java support will be incorporated at the operating system level in
Windows 95 and support capabilities will be extended in Windows NT.
In addition to this three-prong acceptanceend-user tools, development tools, and platform extensions major corporations are coming up with other ways to promote Java. Recently, a venture capital firm and ten leading high-technology
corporations put together a 100 million dollar venture fund to support small companies in the development of new applications for the Java programming language. The Java Fund, managed by the venture firm of Kleiner Perkins Caufield & Byers, includes as
its investors, Sun Microsystems Inc., IBM, and Netscape Communications Corp. Many of these corporations believe that Java's real value will come in the form of more sophisticated applications that will help companies do business over the Internet. That was
the impetus behind the development of the Java Fund.
So you see, the interest is truly there. With all of this support and development activity, Java is sure to have a bright future.
Java has a lot of things going for it. Not only is it a major topic of discussion, but it also has the following:
Of all of these benefits, arguably the most important is that Java is used to create Java applets. Because applets run inside a Web browser, applets have superior independence to many other executable programs or scripting languages. Applets, by their
inherent nature of platform-independent, object-oriented technology and Web browser execution and presentation, will extend the reach of corporate information storage, retrieval, and presentation in ways that are limited only by your imagination. The
following section describes some of these advantages.
Platform independence is one of the most significant advantages that Java has over other programming languages. Unlike some other platform independent languages, such as ANSI C, Java is platform-independent at both the source and the binary level.
Platform-independence is a program's capability of moving from one computer system to another.
At the source level, Java's primitive data types have consistent sizes across all development platforms. Java's foundation class libraries make it easy to write code that can be moved from platform to platform without the need to rewrite it to work
with that platform.
Platform-independence doesn't stop at the source level, however. Java binary files are also platform-independent and can run on multiple platforms without the need to recompile the source. How does this work? Java binary files are actually in a form
called bytecodes.
Bytecodes are a set of instructions that look a lot like machine code, but are not specific to any one processor.
Normally, when you compile a program written in C++, or most other programming languages, the compiler translates your program into machine codes or processor instructions. Those instructions are specific to the processor your computer is running; so,
for example, if you compile your code to run on a 32-bit, Intel-based 80x86 system, the resulting program will run only on other 80x86 systems. If you want to use the same program on another system, you have to go back to your original source, get a
compiler for that system, and recompile your code. This additional compile step is required even though the source code might require little or no changes to run on a different system. This is because the executable program created by the compiler has
specific instructions for the computer system the compiler was designed for. Figure 1.3 shows the result of this system: multiple executable programs for multiple systems.
Figure 1.3. Multiple executable programs for multiple systems.
Things are different when you write code in Java. The Java development environment has two parts: a Java compiler and a Java interpreter. The Java compiler takes your Java program and, instead of generating machine codes from your source files, it
generates bytecodes.
To run a Java program, you run a program called a bytecode interpreter, which in turn executes your Java program. (See Figure 1.4.) You can either run the interpreter by itself, orfor appletsa bytecode interpreter (called the Java virtual
machine), built into Java-capable browsers, can run the applet for you.
Why go through all the trouble of adding this extra layer of the bytecode interpreter? Having your Java programs in bytecode form means that instead of being specific to any one system, your programs can be run on any platform and any operating or
window system, as long as the Java interpreter is available. This capability of a single binary file to be executable across platforms is crucial to what enables applets to work, because the World Wide Web itself is also platform-independent. Just as HTML
files can be read on any platform, so applets can be executed on any platform that is a Java-capable browser. Ponder this power for a moment. . .amazing isn't it? Okay, read on.
The disadvantage of using bytecodes is in execution speed. Because system-specific programs run directly on the hardware for which they are compiled, they run significantly faster than Java bytecodes, which must be processed by the interpreter. For
many Java programs, the speed might not be an issue. If you write programs that require more execution speed than the Java interpreter can provide, you have several solutions available to you, including being able to link native code into your Java program
or using tools to convert your Java bytecodes into native code, which both are beyond the scope of this book. Note that by using any of these solutions, you lose the portability that Java bytecodes provide. Also, remember the future: With the deployment of
just-in-time compilers, the speed issues might become less significant.
To some, object-oriented programming (OOP) technique is merely a way of organizing programs that can be accomplished using any language. Working with a real object-oriented language and programming environment, however, enables you to take full
advantage of object-oriented methodology and its capabilities for creating flexible, modular programs and reusing code.
Many of Java's object-oriented concepts are inherited from C++, the language on which it is based, but it borrows many concepts from other object-oriented languages as well. Like most object-oriented programming languages, Java includes a set of class
libraries that provide basic data types, system input and output capabilities, and other utility functions. These basic classes are part of the Java Development Kit, which also has classes to support accessing network data, common Internet protocols, and
user interface toolkit functions. Because the JDK class libraries are written in Java, they are as portable across platforms as all Java applications.
You'll learn more about object-oriented programming and Java on Day 3.
In addition to its portability and object orientation, one of Java's initial design goals was to be small and simple, and therefore, easy to write, compile, and debug. Keeping the language small also makes it more robust because there are fewer chances
for programmers to make difficult-to-find mistakes. Despite its size and simple design, however, Java still has a great deal of power and flexibility.
Java is modeled after C and C++, and much of the syntax and object-oriented structure is borrowed from the latter. If you are familiar with C++, learning Java will be particularly easy for you, because you have most of the foundation already.
Although Java looks similar to C and C++, most of the more complex parts of those languages have been excluded from Java, making the language simpler without sacrificing much of its power. There are no pointers in Java, nor is there pointer arithmetic.
Strings and arrays are real objects in Java. Memory management is automatic. To an experienced programmer, these omissions might be difficult to get used to, but to beginners or programmers who have worked in other languages, they make the Java language
far easier to learn.
If you're used to using IDEs, using the Java Development Kit and you're own programming editor to create Java applets and applications might seem quite antiquated. Even with the JDK's very simple beginnings, Java still gained amazing popularity. So
popular and so similar to Visual C++ in fact, that Microsoft created VJ++ as the IDE for Java program development. If you're already familiar with Microsoft Developer Studio, the development platform VJ++ is based upon, using the VJ++ IDE will be quite
simple for you. Even if you're not familiar with Microsoft Developer Studio, it will still be easy to develop Java applications using VJ++. Although this book will teach you Java, it will concentrate more on creating Java applets and applications using
VJ++. So, you'll get the benefit of knowing both the language and specifically, the tool.
What is an IDE? An IDE is a self-contained application development environment that contains all the tools you need to write, compile, test, and debug your application. VJ++ also includes InfoView (an expanded online help system, including links to
code examples and classes), object viewers, access to source control packages, and wizards helper applications that walk you through creating certain objects.
Microsoft Developer Studio for VJ++ is a very powerful tool for developing Java programs. It maintains consistency across Microsoft's other development platforms (like VC++), making it easy to learn and use. It is also a full featured IDE with many
extras that the Microsoft tool developers added based on input from the scores of VC++ developers. Figure 1.5 depicts a common work area and some of the key components in VJ++. Notice how the work area is divided into three sections. The pane on the left
is the Project Workspace. It is an index view of the currently selected tab from the bottom of the workspace (currently selected is InfoView). The pane on the right shows the detail pertaining to the selected index item from the left (an Introduction to
Visual J++). The bottom pane is the Output window. It has four tabs: Build, Debug, Find Files, and Profiles. As its name implies, output is placed here by VJ++.
Even if you've never used a Microsoft Developer Studio tool before, by the end of this book you'll be quite a wizard of the desktop, as well as a top-flight Java developer.
Pardon the poetic license of the title. When VJ++ was in development, its code name was Jakarta. The title (Brewing Java in Jakarta) then steeped of alliterative value and island fantasies. We kept it as a fanciful reminder of chicory coffee and sunny beaches.
VJ++ adheres to the tenants of the original JDKnone of the original class libraries have been changed. However, VJ++ goes a lot further. Microsoft Developer Studio itself makes developing and deploying your Java programs easier. The development
environment also includes tools to inspect classes and OLE objects. Microsoft has also added wizards to make creating baseline applets and applications quite easy. The COM extensions and Microsoft Java classes will add OLE automation, ActiveX, and Java
ODBC to your applications. VJ++ will allow you to easily create industry standard portable Java programs, as well as add the new Microsoft extensions.
In order to write VJ++ programs, you will, of course, need Microsoft Visual J++. In order to run VJ++, your development system must be Windows NT 4.0 or Windows 95.
Today, you got a basic introduction to the goals and features of the Java language, and to Microsoft Visual J++. Java is a programming language, similar to C or C++, in which you can develop a wide range of programs. The most common use of Java at the
moment is in creating applets for Java-capable Web browsers such as Microsoft Internet Explorer 3.0 and Netscape 2.0x or greater. Applets are Java programs that are downloaded and run as part of a Web page. Applets can create animation, games, interactive
programs, and other multimedia effects on Web pages.
Java's strengths lie in its portabilityboth at the source and at the binary levelin its object-oriented design, and in its simplicity. Each of these features help make applets possible, but they also make Java an excellent language for
writing more general-purpose programs that do not require Java-capable browsers to run. These general-purpose Java programs are called applications. HotJava itself is a Java application.
You viewed a sample applet and learned the differences and benefits of applets and applications. You also had an initial view of the Microsoft Developer Studio for VJ++. From here, you now have the foundation necessary to proceed with the creation of
applications and applets.
Q: What Web browser should I use to view my Java applets?
A: VJ++ includes the Microsoft Internet Explorer 3.0 browser. It is part of Microsoft Developer Studio for VJ++ and is the tool used to test your applets in a sample Web page.
Q: I know a lot about HTML, but not much about computer programming. Can I still write Java programs?
A: If you have no programming experience whatsoever, you most likely will find programming Java significantly more difficult. However, Java is an excellent language to learn programming with, and if you patiently work through the examples and
the exercises in this book, you should be able to learn enough to get started with Java.
Q: According to today's lesson, Java applets are downloaded via a Java-enabled browser such as Internet Explorer 3.0 and run on the user's system. Isn't that an enormous security hole? What stops someone from writing an applet that compromises the
security of my systemor worse, that damages my system?
A: Sun's Java team has thought a great deal about the security of applets within Java-capable browsers and has implemented several checks to make sure applets cannot do nasty things.
The checks obviously cannot stop every potential security hole (no system can promise that!), but they can significantly reduce the potential for hostile applets. You'll learn more about security on Day 16.
Q: Why use Visual J++?
A VJ++ uses Microsoft Developer Studio, which is the common development platform for all Microsoft development products. It is designed to easily create reusable, object-oriented applets and applications in less time than the JDK by itself. VJ++
maintains the industry standard JDK 1.0 specification, while providing new technologies such as COM. The VJ++ InfoView provides a help system with code and class examples, which also integrates with the debugger for trouble-shooting any errors your
programs might encounter. By using VJ++ you will be able to produce amazing things in less time, with fewer errors.