- Shopping Bag ( 0 items )
This book is a comprehensive introduction to the Java programming language, updated thoroughly (more than 35% new and updated) for the latest SDK 1.5 release. This book shows readers how to build real-world Java applications using the Java SDK. No previous programming experience is required. The author uses numerous step-by-step programming examples to guide readers through the ins and outs of Java development. In addition to fully covering new features ...
Ships from: Scarborough, Canada
Usually ships in 1-2 business days
This book is a comprehensive introduction to the Java programming language, updated thoroughly (more than 35% new and updated) for the latest SDK 1.5 release. This book shows readers how to build real-world Java applications using the Java SDK. No previous programming experience is required. The author uses numerous step-by-step programming examples to guide readers through the ins and outs of Java development. In addition to fully covering new features of SDK 1.5, such as generic types, the author has also added new chapters on Java database programming with JDBC and Java programming with XML.
This chapter will give you an appreciation of what the Java language is all about. Understanding the details of what I'll discuss in this chapter is not important at this stage; you will see all of the topics 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 I'll be covering through the rest of the book, as well as the major 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:
The basic characteristics of the Java language How Java programs work on your computer Why Java programs are portable between different computers The basic ideas behind object-oriented programming How a simple Java program looks and how you can run it using the Java Development Kit What HTML is and how it is used to include a Java program in a web page
What Is Java All About?
Java is an innovative programming language that has become the language of choice for programs that need to run on a variety of different computer systems. First of all, Java enables you to write small programs called applets. These are programs that you can embed in web pages to provide some intelligence. 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 almost unlimited.
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 won't 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 any Java code that was created with malicious intent is effectively inhibited. Java implicitly incorporates measures to minimize the possibility of such occurrences arising with a Java applet.
Java's support for the Internet and network-based applications generally doesn't end with applets. For example, Java Server Pages (JSP) provides a powerful means of building a server application that can dynamically create and download HTML pages to a client that are precisely customized for the specific request that is received. Of course, the pages that are generated by JSP can themselves contain Java applets.
Java also allows you to write large-scale application programs that you can run unchanged on any computer with an operating system environment in which Java is supported. This applies to the majority of computers in use today. You can even write programs that will work both as ordinary applications and as applets.
Java has matured immensely in recent years, particularly since the introduction of Java 2. The breadth of function provided by the standard core Java has grown incredibly. Java provides you with comprehensive facilities for building applications with an interactive graphical user interface (GUI), extensive image processing and graphics programming facilities, as well as support for accessing relational databases and communicating with remote computers over a network. Just about any kind of application can now be programmed effectively in Java, with the implicit plus of complete portability.
Of course, Java is still developing and growing. Amongst a myriad of other enhancements, release 1.4 of Java added a major additional capability, the ability to read and write XML. Java 5.0, which followed release 1.4, adds further new facilities, including important new language features as well as significant additions to the class libraries. You'll be learning about all of these in this book.
Features of The Java Language
The most important characteristic of Java is that it was designed from the outset to be machine independent. You can run Java programs unchanged on any machine and operating system combination 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 application written in Java will only require a single set of source code statements, regardless of the number of different computer platforms on which it is run. In any other programming language, the application will frequently require the source code to be tailored to accommodate different computer environments, particularly if an extensive graphical user interface is involved. Java offers substantial savings in time and resources in developing, supporting, and maintaining major applications on several different hardware platforms and operating systems.
Possibly the next most important characteristic of Java is that it is object-oriented. The object-oriented 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. Object-oriented programs are easier to understand and less time-consuming to maintain and extend than programs that have been written without the benefit of using objects.
Not only is Java object-oriented, but it also manages to avoid many of the difficulties and complications that are inherent in some other object-oriented languages, making it easy to learn and very straightforward to use. By and large, it lacks the traps and "gotchas" that arise in some other programming languages. This makes the learning cycle shorter, and you need less real-world coding experience to gain competence and confidence. It also makes Java code easier to test.
Java has a built-in ability to support national character sets. You can write Java programs as easily for use in Greece or Japan as you can for English-speaking countries, always assuming you are familiar with the national languages involved, of course. You can even build programs from the outset to support several different national languages with automatic adaptation to the environment in which the code executes.
Java is not difficult to learn, but there is a great deal to it. Although the Java language is very powerful, it is fairly compact, so acquiring an understanding of it will take less time than you think. However, there's more to Java than just the language. To be able to program effectively in Java, 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'll gain expertise and confidence with programming in Java through a relatively easy and painless process. As far as possible, each chapter avoids the use of things you haven't learned about already. A consequence, though, is that you won't be writing Java applications with a GUI 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, you'll minimize the chance of drowning, and there is a high expectation that you'll end up being a competent swimmer.
As I have already noted, there are two basic kinds of programs you can write in Java. Programs that are to be embedded in a web page are called Java applets, and normal standalone programs are called Java applications. You can further subdivide Java applications into console applications, which only support character output to your computer screen (to the command line on a PC under Windows, for example), and windowed applications, which can create and manage multiple windows. The latter use the typical GUI mechanisms of window-based programs-menus, toolbars, dialogs, and so on.
While you are learning the Java language basics, you will be using console applications as examples to illustrate how things work. These are applications that use simple command-line input and output. With this approach you can concentrate on understanding 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, you'll move on to windowed applications and applet examples.
Learning Java-The Road Ahead
Before starting out on any journey, it is always helpful to have an idea of where you're heading and what route you should take, so let's take a look at a brief road map of where you'll be going with Java. There are five broad stages you'll 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 object-oriented 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'll 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'll learn about classes-how you define them and how you can use them. Classes are blueprints for objects, so this is where you'll learn the object-oriented characteristics of Java. By the time you are through this stage, you will have learned all the basics of how the Java language works, so you'll be ready to progress further into how you can use it. 4. In the fourth stage, you'll learn how you can segment the activities that your programs carry out into separate tasks 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'll 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. 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.
Throughout this book I'll be using complete examples to explore how Java works. You should create and run all of the examples, even the simplest, preferably by typing them in yourself. Don't be afraid to experiment with them. If there is anything you are not quite clear on, try changing an example around to see what happens, or better still-write an example of your own. If you're uncertain how some aspect of Java that you have already covered works, don't look it up right away-try it out. Making mistakes is a very effective way to learn.
The Java Environment
You can run Java programs on a wide variety of computers using a range of operating systems. Your Java programs will run just as well on a PC running any supported version of Microsoft Windows as it will on Linux or a Sun Solaris workstation. This is possible because a Java program does not execute directly on your computer. It runs on a standardized environment called the Java 2 Platform that has been implemented as software on a wide variety of computers and operating systems. The Java Platform consists of two elements-a software implementation of a hypothetical computer called the Java Virtual Machine (JVM) and the Java Application Programming Interface (Java API), which is a set of software components that provides the facilities you need to write a fully fledged interactive application in Java.
A Java compiler converts the Java source code that you write into a binary program consisting of byte-codes. Bytecodes are machine instructions for the Java Virtual Machine. When you execute a Java program, a program called the Java interpreter inspects and deciphers the bytecodes for it, checks it out to ensure that it has not been tampered with and is safe to execute, and then executes the actions that the bytecodes specify within the Java Virtual Machine. A Java interpreter can run standalone, or it can be part of a web browser such as Netscape Navigator, Mozilla, or Microsoft Internet Explorer where it can be invoked automatically to run applets in a web page.
Because your Java program consists of bytecodes rather than native machine instructions, it is completely insulated from the particular hardware on which it is run. Any computer that has the Java environment implemented will handle your program as well as any other, and because the Java interpreter sits between your program and the physical machine, it can prevent unauthorized actions in the program from being executed.
In the past, there has been a penalty for all this flexibility and protection in the speed of execution of your Java programs. An interpreted Java program would typically run at only one-tenth of the speed of an equivalent program using native machine instructions. With present Java machine implementations, much of the performance penalty has been eliminated, and in programs that are not computation intensive-which is usually the case with the sort of program you would want to include in a web page, for example-you really wouldn't notice this anyway. With the JVM that is supplied with the current Java 2 Development Kit (JDK) available from the Sun web site, there are very few circumstances where you will notice any appreciable degradation in performance compared to a program compiled to native machine code.
Java Program Development
For this book you need the Java 2 Platform, Standard Edition (J2SE) version 5.0 or later. You can download the JDK from Sun for a variety of hardware platforms and operating systems, either directly from the Sun Java web site (for Windows, Solaris, and Linux operating systems) or from sites that you can link to from there. The JDK you'll be using is available from java. sun.com/j2se. Versions of the Java Development Kit for Mac OS X are available from devworld.apple.com/java/.
Note that J2SE 5.0 succeeded J2SE 1.4. Normally, release 1.5 would have followed release 1.4, but it was decided to identify it as release 5.0 in recognition of the significance of the new features that are introduced by release 5.0 and the maturity of the product. Code module names in release 5.0 still use the denotation 1.5.0 so expect to see folder names incorporating 1.5.0 rather than 5.0, and you'll see 1.5.0 popping up in a few other places too, so don't let this confuse you.
One aspect of terminology also causes confusion sometimes-the Java Development Kit has been referred to at various times as the JDK-the Java Development Kit-and as the SDK-the Software Development Kit. The current usage with release 5.0 is JDK but with release 1.4 it was SDK, so if you see SDK this generally means the same as JDK. Just for consistency, I'll use JDK to refer to any Java Development Kit in the book.
To create the Java program source files that you will use with the JDK, you'll need a plaintext editor. Any editor will do as long as it does not introduce formatting codes into the contents of a file. Quite a number of shareware and freeware editors around are suitable, some of which are specific to Java, and you should have no trouble locating one. I find the JCreator editor is particularly good. There's a free version and a fee version with more functionality, but the free version is perfectly adequate for learning. You can download a free copy from jcreator.com. A good place to start looking if you want to investigate what other program text editors are available is the download.com web site.
Excerpted from Ivor Horton's Beginning Java 2 by Ivor Horton Excerpted by permission.
All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.
|Ch. 1||Introducing Java||1|
|Ch. 2||Programs, data, variables, and calculation||29|
|Ch. 3||Loops and logic||85|
|Ch. 4||Arrays and strings||135|
|Ch. 5||Defining classes||199|
|Ch. 6||Extending classes and inheritance||269|
|Ch. 8||Understanding streams||371|
|Ch. 9||Accessing files and directories||401|
|Ch. 10||Writing files||427|
|Ch. 11||Reading files||483|
|Ch. 12||Serializing objects||525|
|Ch. 13||Generic class types||547|
|Ch. 14||The collections framework||601|
|Ch. 15||A collection of useful classes||659|
|Ch. 17||Creating windows||767|
|Ch. 18||Handling events||861|
|Ch. 19||Drawing in a window||927|
|Ch. 20||Extending the GUI||997|
|Ch. 21||Filing and printing documents||1079|
|Ch. 22||Java and XML||1151|
|Ch. 23||Creating and modifying XML documents||1223|
|Ch. 24||Talking to databases||1275|
|Ch. 25||The JDBC in action||1329|
|App. B||Computer arithmetic||1401|