Read an Excerpt
Chapter 1: Introducing JavaThis chapter will give you an appreciation of what the Java language is all about. Understanding the details that we'll introduce in this chapter is not important at this stage; you will see all of them again in greater depth in later chapters of the book. The intent of this chapter is to introduce you to the general ideas that underpin what we'll be covering through the rest of the book, as well as the contexts in which Java programs can be used and the kind of program that is applicable in each context.
In this chapter you will learn:
What is Java All About?
Java is an innovative programming language that first of all enables you to write programs called applets that you can embed in Internet Web pages. Java also allows you to write applications programs that you can run normally on your, or indeed any, computer that supports the language. You can even write programs that will work as an ordinary application and as an applet. Because Java has matured so much recently, it is becoming the language of choice for applications that need to run on a variety of different computer systems.
Being able to embed executable code in a Web page introduces a vast range of exciting possibilities. Instead of being a passive presentation of text and graphics, a Web page can be interactive in any way that you want. You can include animations, games, interactive transaction processing - the possibilities are unlimited.
Beginning Java 2
Of course, embedding program code in a Web page creates special security requirements. As an Internet user accessing a page with embedded Java code, you need to be confident that it will not do anything that might interfere with the operation of your computer, or damage the data you have on your system. This implies that execution of the embedded code must be controlled in such a way that it will prevent accidental damage to your computer environment, as well as ensure that creating code with malicious intent is effectively inhibited. Java implicitly incorporates measures to minimize the possibility of such occurrences.
Aside from its ability to create programs that can be embedded in a Web page, perhaps the most important characteristic of Java is that it was designed from the outset to be machine independent. Java programs will run unchanged on an); computer that supports Java. Of course there is still the slim possibility of the odd glitch as you are ultimately dependent on the implementation of Java on any particular machine, but Java programs are intrinsically more portable than programs written in other languages. An interactive application written in Java will only require a single set of source code, regardless of the number of different computer platforms on which it is run. In any other programming language the application will require the source code to be tailored to each individual computer to accommodate the particular idiosyncrasies of that machine and operating system.
Possibly the next most important characteristic of Java is that it is object oriented. The objectoriented approach to programming is also an implicit feature of all Java programs, so we will be looking at what this implies later in this chapter. Not only is Java object oriented, but it manages to avoid many of the difficulties and complications that are inherent in some other object-oriented languages, so you will find it very straightforward and easy to learn.
Java is not difficult, but there is a great deal to it. The language itself is quite compact, but very powerful. To be able to program effectively in Java, however, you also need to understand the libraries that go with the language, and these are very extensive. In this book, the sequence in which you learn how the language works, and how you apply it, has been carefully structured so that you can gain expertise and confidence with programming in Java through a relatively easy and painless process. Each chapter, as far as possible, avoids the use of things you haven't learnt about already. A consequence, though, is that you won't be writing Java to be embedded in Web pages right away. While it may be an appealing idea, this would be a bit like learning to swim by jumping in the pool at the deep end. Generally speaking, there is good evidence that by starting in the shallow end of the pool and learning how to float before you try to swim, the chance of you drowning is minimized, and there is a high expectation that you will end up a competent swimmer.
As we have already noted, there are two kinds of programs you can write in Java. Programs that are to be embedded in a Web page are called Java applets, and ordinary standalone programs are called Java applications. You can further subdivide Java applications into console applications, which only support character output to your computer screen (in a DOS window on a PC under Windows, for example) and windowed Java applications that can create and manage multiple windows, and use the typical graphical user interaction (GUI) mechanisms of window-based programs.
While you are learning the Java language basics, we will be using console applications as examples to illustrate how things work. This is because we can then focus on the specifics of the language, without worrying about any of the complexity involved in creating and managing windows. Once you are comfortable with using all the features of the Java language, we'll move on to windowed applications and applet examples.
Learning Java - the Road Ahead
Before starting out, it is always helpful to have an idea of where you are heading and what route you should take, so let's take a look at a brief road map of where you will be going with Java. There are five broad stages you will progress through in learning Java using this book:
1. The first stage is this chapter. It sets out some fundamental ideas about the structure of Java programs and how they work. This includes such things as what objectoriented programming is all about, and how an executable program is created from a Java source file. Getting these concepts straight at the outset will make learning to write Java programs that much easier for you.
2. Next you will learn how statements are put together, what facilities you have for storing basic data in a program, how you perform calculations and how you make decisions based on the results of them. These are the nuts and bolts you need for the next stages.
3. In the third stage you will learn about classes - how you define them and how you can use them. This is where you learn the object-oriented characteristics of the language. By the time you are through this stage you will have learnt all the basics of how the Java language works so you will be ready to progress further into how you can use it.
4. In the fourth stage, you will learn how you can segment your application into pieces that can execute concurrently. This is particularly important for when you want to include several applets in a Web page, and you don't want one applet to have to wait for another to finish executing, before it can start. You may want a fancy animation to continue running while you play a game, for example, with both programs sitting in the same Web page.
5. In the fifth stage you will learn in detail how you implement an application or an applet with a graphical user interface, and how you handle interactions with the user in this context. This amounts to applying the capabilities provided by the Java class libraries. As well as learning about GUI implementation, you will also learn about handling images in various ways, as well as using the database access facilities in Java. When you finish this stage you will be equipped to write your own fully-fledged applications and applets in Java. At the end of the book, you should be a knowledgeable Java programmer. The rest is down to experience....