Owl Sams Teach Yourself Java 1.1 Programming in 24 Hours
Because the 24 Hours line is new, you might not have a good feel for what these books are about. The following text is a reproduction of the first several sections of Hour 1. Read it for an introduction to computer programming and how the book will teach Java 1.1.

Becoming a Programmer

From Hour 1, Teach Yourself Java 1.1 Programming in 24 Hours by Rogers Cadenhead

Computer programming is insanely difficult. It requires a four-year degree in computer science, thousands of dollars in computer hardware and software, a keen analytical intellect, the patience of Job, and a strong liking for caffeinated drinks. If you're a programming novice, this is probably what you've heard about computer programming. Aside from the part about caffeine, all of the rumors are greatly exaggerated.

Programming is a lot easier than most people think. There are several reasons why you might believe otherwise:

  • Computer programmers have been telling people for years that programming is hard. This belief makes it easier for us to find high-paying jobs (or so I've heard) and gives us more leeway to goof off during business hours.
  • Computer programming manuals are often written in a language that only a Scrabble player could appreciate. Strange acronyms like OOP, RAD, COM, and MUMPS are used frequently along with newly invented jargon like instantiation, bytecode, and makefile.
  • Many computer programming languages have been available only with software packages costing $200 or more, which is a lot of cabbage.

Because of the growth of the Internet and other factors, this is a great time to learn programming. Useful programming tools are being made available at low cost (or no cost), often as downloads from World Wide Web sites. The goal of this book is to teach programming to the person who has never tried to program before or the person who tried programming but hated it with an intense passion. The English language will be used as much as possible instead of jargon and obscure acronyms, and all new programming terms will be thoroughly explained as they are introduced.

If I've succeeded, you will finish Teach Yourself Java 1.1 Programming in 24 Hours with enough programming skill to be a danger to yourself and others. You'll be able to write programs, dive into other programming books with more confidence, and learn programming languages more easily. You also will have developed skills with Java, the most exciting programming language to be introduced in a decade.

The first hour of this book provides some introductory material about programming and gives you instructions on how to set up your computer so you can write Java programs. The following topics will be covered [in this excerpt]:

  • Choosing which programming language to learn first
  • Using programs to boss your computer around
  • How programs work
  • How program errors called bugs are fixed

Choosing a Language

As you might have surmised at this point, computer programming is not as hard as it's cracked up to be. If you're comfortable enough with a computer to create a nice-looking resume, balance a checkbook with software such as Intuit Quicken, or create your own home page on the Web, you can write programs.

The key to learning how to program is to start with the right language. The programming language that you choose to use often depends on the tasks you want the computer to accomplish. Each language has things that it is well-suited for and things that are difficult, or perhaps impossible, to do with the language. For example, many people use some form of the BASIC language when they are learning how to program because BASIC is good for learning how to write programs.

Just a Minute

The BASIC language was invented in the '60s to be easy for students and beginners to learn (the B in BASIC stands for Beginner's). The downside to using some form of BASIC is that it's easy to fall into some sloppy programming habits with the language. Those habits can make it much more difficult to write complex programs and improve them later on.

Microsoft Visual Basic combines the ease of BASIC with some powerful features to aid in the design of Windows software. (VBScript, which is short for Visual Basic Script, offers the simplicity of BASIC for small programs that run in conjunction with World Wide Web pages.) Visual Basic has been used to write thousands of sophisticated programs in commercial, business, and personal use. However, Visual Basic programs can be slower than Windows programs written in other languages such as Borland C++. This difference is especially noticeable in programs that use a lot of graphics -- games, screen savers, and the like. Because of that, game programmers and other multimedia developers don't use Visual Basic to create graphical programs such as Doom.

This book uses the Java programming language. Though Java is more difficult to learn than a language such as Visual Basic, it is a good starting place for several reasons. One of the biggest advantages of learning Java is that you can use it on the World Wide Web. If you're an experienced Web surfer, you have seen numerous Java programs in action. They can be used to create animated graphics, present text in new ways, play games, and help in other interactive efforts.

Another important advantage is that Java requires an organized approach in order for programs to work. The language is very particular about the way that programs must be written, and it balks if programmers do not follow all of its rules. When you start writing Java programs, you might not see the language's choosy behavior as an advantage. You'll write a program and will have several errors to fix before the program will be finished. Some of your fixes might not be correct, and they will have to be redone. If you don't structure a program correctly as you are writing it, other errors will result. In the coming hours, you'll learn about these rules and the pitfalls to avoid. The positive side of this extra effort is that your programs will be more reliable, useful, and error-free.

Java was invented by Sun Microsystems developer James Gosling as a better way to create computer programs. Gosling was unhappy with the way that the C++ programming language was working on a project he was doing, so he created a new language that did the job better. It's a matter of contentious debate whether Java is superior to other programming languages, of course, but the amount of attention paid to the language today shows that it has a large number of adherents. Book publishers obviously dig it -- more than 89 books have been published about the language since its introduction. (This is my second book. Teach Yourself SunSoft Java WorkShop in 21 Days was the first, and I will write more of them until prohibited by municipal, state, or federal law.) Regardless of whether Java is the best language, it definitely is a great language to learn today. There are numerous resources for Java programmers on the Web, Java job openings are increasing, and the language has become a major part of the Internet's future. You'll get a chance to try out Java during Hour 2, "Writing Your First Program."

Learning Java or any other programming language makes it much easier to learn other languages. Many languages are similar to each other, so you won't be starting from scratch when you dive into a new one. For instance, many C++ programmers find it fairly easy to learn Java because Java borrows a lot of its structure and ideas from C++. Many programmers are comfortable using several different languages and will learn new ones as needed.

Just a Minute

C++ has been mentioned several times in this hour, and you might be tripping over the term wondering what it means and, more importantly, how it's pronounced. C++ is pronounced C-Plus-Plus, and it's a programming language that was developed by Bjarne Stroustrop and others at Bell Laboratories. C++ is an enhancement of the C programming language, hence the Plus-Plus part of the name. Why not just C+, then? The Plus-Plus part is a computer programming joke you'll understand later on.

Telling the Computer What to Do

A computer program, also called software, is a way to tell a computer what to do. Everything that the computer does, from booting up to shutting down, is done by a program. Windows 95 is a program. Ms. Pac-Man is a program. The dir command used in MS-DOS to display file names also is a program. Even the Michaelangelo virus is a program.

Computer programs are made up of a list of commands that the computer handles in a specific order when the program is run. Each of these commands is called a statement.

If you're a science fiction fan, you're probably familiar with the concept of household robots. If not, you might be familiar with the concept of henpecked spouses. In either case, someone gives very specific instructions telling the robot or spouse what to do, something like the following:

Dear Theobald,
 
Please take care of these errands for me while I'm out lobbying members of Congress:
 
Item 1: Vacuum the living room.
Item 2: Go to the store.
Item 3: Pick up butter, lozenges, and as many SnackWells Devil's Food Cakes as you can carry.
Item 4: Return home.
 
Love,
Snookie Lumps

If you tell a loved one or artificially intelligent robot what to do, there's a certain amount of leeway in how your requests are fulfilled. If lozenges aren't available, cough medicine might be brought to you instead. Also, the trip to the store can be accomplished through a variety of routes. Computers don't do leeway. They follow instructions literally. The programs that you write will be followed precisely, one statement at a time.

The following is one of the simplest examples of a computer program, written in BASIC. Take a look at it, but don't worry yet about what each line is supposed to mean:

1 PRINT "Shall we play a game?"

2 INPUT A$

Translated into English, this program is equivalent to giving a computer the following to-do list:

Dear personal computer,
 
Item 1: Display the question, "Shall we play a game?"
Item 2: Give the user a chance to answer the question.
 
Love,
Snookie Lumps

Each of the lines in the computer program is a statement. A computer handles each statement in a program in a specific order, in the same way that a cook follows a recipe or Theobald the robot followed the orders of Snookie Lumps when he vacuumed and shopped at the market. In BASIC, the line numbers are used to put the statements in the correct order. Other languages, such as Java, do not use line numbers, favoring different ways to tell the computer how to run a program.

Figure 1.1 shows the sample BASIC program running on the Bywater BASIC interpreter, which is available for free in several shareware file repositories on the World Wide Web and can run on any DOS or UNIX platform. Bywater BASIC is among many free BASIC interpreters that can be found on the Internet for Microsoft Windows, Apple Macintosh, UNIX, and Linux systems.

Figure 1.1. An example of a BASIC program running on the Bywater BASIC shell and interpreter developed by Ted A. Campbell.

Just a Minute

The quote "Shall we play a game?" is from the 1983 movie WarGames, in which a young computer programmer (Matthew Broderick) saves mankind after nearly causing global thermonuclear war and the near-extinction of humankind. You'll learn how to do that in the next book of this series, Teach Yourself to Create International Incidents with Java in 24 Hours.

Because of the way programs operate, it's hard to blame the computer when something goes wrong while your program runs. After all, the computer was just doing exactly what you told it to do. Unless your hardware is on the fritz or a pesky virus is attacking your system, both rare occurrences, the blame for program errors lies with the programmer. That's the bad news. The good news is that you can't do any permanent harm to your computer with the programming errors you make. No one was harmed during the making of this book, and no computers will be injured as you learn how to program with Java.

How Programs Work

Most computer programs are written in the same way that you write a letter -- by typing each statement into a word processor. Some programming tools come with their own word processor, and others can be used with any text-editing software. You can use the Java Developer's Kit, which you will learn about later in [Hour 1], with any of your favorite editors.

When you have finished writing a computer program, you save the file just like saving any other document to disk. Computer programs often have their own file extension to indicate what type of file they are. Java programs have the extension .java; an example of a Java program file name is Calculator.java.

Just a Minute

If you use a fancy word processing program that has features such as boldfaced text, different font sizes, and other stylistic touches, do not use those features while writing a computer program. Programs should be prepared as text files with no special formatting. For example, when using Microsoft Word to write a program, save the file in Text Only mode instead of saving it as a Word document. Notepad, a word processor that comes with Windows, saves all files as unformatted text.

For this program to run, you need some help. The kind of help that's needed depends on the programming language you're using. Some languages require an interpreter to run their programs. The interpreter is a program that interprets each line of a computer program and tells the computer what to do. Most versions of BASIC are interpreted languages. The advantage of interpreted languages is that they are faster to test. When you are writing a BASIC program, you can try it out immediately, spot any errors, fix them, and try again. The primary disadvantage is that interpreted languages run more slowly than other programs.

Other programming languages require a compiler. The compiler takes a computer program and translates it into a form that the computer can understand. It also does what it can to make the program run as efficiently as possible. The compiled program can be run directly, without the need for an interpreter. Compiled programs run more quickly than interpreted programs, but they take more time to test. You have to write your program and compile it before trying it out. If you find an error and fix it, you must compile the program again to verify that the error is gone.

Java is unusual because it requires a compiler and an interpreter. You'll learn more about this later as you write Java programs.

How Programs Don't Work

Many new programmers become discouraged when they start to test their programs. Errors appear everywhere. Some of these are syntax errors, which are identified by the computer as it looks at the program and becomes confused by what you wrote. Other errors are logic errors, which are only noticed by the programmer as the program is being tested, if they are noticed at all. Logic errors sneak by the computer unnoticed, but they will cause it to do something unintended.

As you start to write your own programs, expect to encounter errors. They're a natural part of the process. Programming errors are called bugs, a term that dates back a century or more to describe errors in technical devices. The process of fixing errors has its own term also: debugging. Whether you want to or not, you'll get a lot of debugging experience as you learn how to write computer programs.

Summary

During this hour, you were introduced to the concept of programming a computer -- giving it a set of instructions that tell it what to do. ...

If you are still confused about programs, programming languages, or Java in general, that's understandable at this point. Everything will make more sense to you in the next hour, "Writing Your First Program," which takes a slow trip through the process of creating a Java program.

Q&A

Q: What does the Internet have to do with making it easier to learn programming?

A: Because of the dramatic growth of the World Wide Web, companies such as Microsoft, Netscape, and Sun Microsystems are trying to attract as many programmers as possible to their languages and related technology. To do this, they are offering many programming tools for free over the Web, such as the Java Developer's Kit and the beta release of the Microsoft Visual Basic 5 Control Creation Edition, and are offering others for free 30- or 90-day trial periods, such as SunSoft Java WorkShop. There also are numerous free products distributed over the Internet for programmers. To find out where these products are, visit Yahoo! at http://www.yahoo.com and search for a language you're interested in. It's much cheaper today to learn programming than it was five years ago.

Q: What is it about BASIC that makes it easier to fall into bad habits while writing programs?

A: One thing you'll learn as you start writing Java programs is that you have to be organized. If you don't structure your program in the correct way, it won't work. BASIC doesn't have this kind of requirement. You can write in a disorganized manner and still get the program to work successfully. Later on, however, you'll have a much harder time figuring out the program if you try to fix a bug or add an improvement.

Q: BASIC? C++? Java? What are the names of these languages supposed to mean?

A: Like many programming languages, BASIC is an acronym that describes what it is: Beginner's All Symbolic Instruction Code. C++ is a programming language that was created to be an improvement on the C language, which itself was an improvement of the B programming language. Java goes against the tradition of naming a language with an acronym or other meaningful term. It's just the name that Java's developers liked the best when brainstorming for possible monikers -- beating out WebRunner, Silk, Ruby, and others.

Q: Why are interpreted languages slower than compiled ones?

A: For the same reason that a person interpreting a live speech is a lot slower than a translator interpreting the printed speech later on. The live interpreter has to think about each statement that's being made as it happens, while the other interpreter can work on the speech as a whole and take some shortcuts to speed up the process. Compiled languages can be much faster than interpreted languages because they can do things to make the program more efficient.

Q: Is C++ harder to learn than Java?

A: It's a matter of personal opinion, but Java does seem more approachable for beginners than C++. C++ and its predecessor, C, are widely regarded as "programmer's languages," meaning that they were designed for the needs of experienced programmers. There are a lot of features in C and C++ that make them faster -- and more powerful -- during program creation, but these features often come at the expense of understandability. Java takes a more simplified approach to programming than C++ and is probably a better place to start.

Quiz

Test your knowledge of the material covered in this chapter by answering the following questions.

Questions

1. Which of the following is not a reason that people think computer programming is painfully difficult?

  • (a) Programmers spread that rumor to improve their employment prospects.
  • (b) Jargon and acronyms are all over the place.
  • (c) Mind-control waves are sent out by the CIA promoting this belief.

2. What kind of tool runs a computer program by figuring out one line at a time?

  • (a) A slow tool
  • (b) An interpreter
  • (c) A compiler

3. Why did James Gosling hole up in his office and create Java?

  • (a) He was unhappy with the language he was using on a project.
  • (b) His rock band wasn't getting any gigs.
  • (c) When you can't download any image files at work, the World Wide Web is pretty dull.

Answers

1. c. Of course, the CIA could have forced me to say this.

2. b. Compilers figure out the instructions beforehand so the program can run faster.

3. a. The Web was still a little-known idea when Gosling wrote Java.

Activities

If you'd like to better introduce yourself to Java before you create your first program, do the following activity:

  • Visit the JavaSoft site at http://java.sun.com and read some of the introductory articles that are presented in the What is Java? section.