Chapter 2

All about Software

In This Chapter

arrow Understanding the roles of the software development tools

arrow Selecting the version of Java that’s right for you

arrow Preparing to write and run Java programs

The best way to get to know Java is to do Java. When you’re doing Java, you’re writing, testing, and running your own Java programs. This chapter gets you ready to do Java by describing the general software setup — the software that you must have on your computer whether you run Windows, Mac, Linux, or Joe’s Private Operating System. This chapter doesn’t describe the specific setup instructions for Windows, for a Mac, or for any other system.

ontheweb.eps For setup instructions that are specific to your system, visit this book’s website.

Quick-Start Instructions

If you’re a seasoned veteran of computers and computing (whatever that means), and if you’re too jumpy to get detailed instructions from this book’s website, you can try installing the required software by following this section’s general instructions. The instructions work for many computers, but not for all computers. And this section provides no detailed steps, no if-this-then-do-that alternatives, and no this-works-but-you’re-better-off-doing-something-else tips.

To prepare your computer for writing Java programs, follow these steps:

check.png Visit Java.com.

Follow the instructions at http://java.com/en to download and install Java.

check.png Optionally, visit java.sun.com/javase/downloads/.

Follow the instructions at that website to download and install the Java SE documentation (also known as the Javadoc pages or the Java SE API Docs).

check.png Visit Eclipse.org.

Follow the instructions at http://eclipse.org/downloads/ to download and install Eclipse.

tip.eps Eclipse’s download page offers several different packages, including Eclipse Classic, Eclipse for Java EE, Eclipse for JavaScript, and others. To run this book’s examples, you need a relatively small Eclipse package — the Eclipse IDE for Java Developers.

check.png Test your installed software.

• Launch Eclipse.

• In Eclipse, create a new Java project.

• Within the Java project, create a new Java class named Displayer.

• Edit the new Displayer.java file by typing the code from Listing 3-1 (the first code listing in Chapter 3). Type the code in Eclipse’s editor pane.

• Run Displayer.java and check to make sure that the run’s output reads You’ll love Java!.

That’s it! But remember, not everyone (computer geek or not) can follow these skeletal instructions flawlessly. So you have several alternatives:

check.png Visit this book’s website.

Do not pass “go.” Do not try this section’s quick-start instructions. Follow the more detailed instructions that you find at www.allmycode.com/JavaForDummies.

check.png Try this section’s quick-start instructions.

You can’t hurt anything by trying. If you accidentally install the wrong software, you can probably leave the wrong software on your computer. (You don’t have to uninstall it.) If you’re not sure whether you’ve installed the software correctly, you can always fall back on my website’s detailed instructions.

check.png E-mail your questions to me at [email protected].

I like getting e-mail from readers.

What You Install on Your Computer

I once met a tool and die maker. He used tools to make tools (and dies). I was happy to meet him because I knew that, one day, I’d make an analogy between computer programmers and tool and die makers.

A computer programmer uses existing programs as tools to create new programs. The existing programs and new programs might perform very different kinds of tasks. For example, a Java program (a program that you create) might keep track of a business’s customers. To create that customer-tracking program, you might use an existing program that looks for errors in your Java code. This general-purpose error-finding program can find errors in any kind of Java code — customer-tracking code, weather-predicting code, gaming code, or the code for an app on your mobile phone.

So how many tools do you need for creating Java programs? As a novice, you need three tools.

check.png You need a compiler.

A compiler takes the Java code that you write and turns that code into something that can run on your computer.

check.png You need a Java virtual machine (JVM).

A Java virtual machine runs your code (and other peoples’ Java code) on your computer.

check.png You need an integrated development environment (IDE).

An integrated development environment helps you manage your Java code and provides convenient ways for you to write, compile, and run your code.

The World Wide Web has free, downloadable versions of each of these tools. For example, the quick-start instructions near the beginning of this chapter advise you to visit Java.com and Eclipse.org. By clicking a button on a Java.com page, you install a Java virtual machine on your computer. At Eclipse.org, you download the Eclipse integrated development environment, which comes with its own built-in Java compiler. (You get two of the three tools in one download. Not bad!)

The rest of this chapter describes compilers, JVMs, and IDEs.

ontheweb.eps The rest of this chapter provides background information about software you need on your computer. But the chapter contains absolutely no detailed instructions to help you install the software. For detailed instructions, visit this book’s website.

What is a compiler?

“A compiler takes the Java code that you write, and turns that code into something that can run on your computer.”

–Barry Burd, Java For Dummies, 5th Edition

You’re a human being. (Sure, every rule has exceptions. But if you’re reading this book, you’re probably human.) Anyway, humans can write and comprehend the code in Listing 2-1.

Listing 2-1: Looking for a Vacant Room

// This is part of a Java program

// (not a complete Java program).

roomNum = 1;

while (roomNum < 100) {

if (guests[roomNum] == 0) {

out.println(“Room “ + roomNum

+ “ is available.”);

exit(0);

} else {

roomNum++;

}

}

out.println(“No vacancy”);

The Java code in Listing 2-1 checks for vacancies in a small hotel (a hotel with room numbers 1 to 99). You can’t run the code in Listing 2-1 without adding several additional lines. But here in Chapter 2, those additional lines aren’t important. What’s important is that, by staring at the code, squinting a bit, and looking past all the code’s strange punctuation, you can see what the code is trying to do:

Set the room number to 1.

As long as the room number is less than 100,

Check the number of guests in the room.

If the number of guests in the room is 0, then

report that the room is available,

and stop.

Otherwise,

prepare to check the next room by

adding 1 to the room number.

If you get to the non-existent room number 100, then

report that there are no vacancies.

If you don’t see the similarities between Listing 2-1 and its English equivalent, don’t worry. You’re reading Java For Dummies, 5th Edition, and like most human beings, you can learn to read and write the code in Listing 2-1. The code in Listing 2-1 is called Java source code.

So here’s the catch: Computers aren’t human beings. Computers don’t normally follow instructions like the instructions in Listing 2-1. That is, computers don’t follow Java source code instructions. Instead, computers follow cryptic instructions like the ones in Listing 2-2:

Listing 2-2: The Instructions of Listing 2-1 Translated into Java Bytecode

aload_0

iconst_1

putfield Hotel/roomNum I

goto 32

aload_0

getfield Hotel/guests [I

aload_0

getfield Hotel/roomNum I

iaload

ifne 26

getstatic java/lang/System/out Ljava/io/PrintStream;

new java/lang/StringBuilder

dup

ldc “Room “

invokespecial java/lang/StringBuilder/<init>(Ljava/lang/String;)V

aload_0

getfield Hotel/roomNum I

invokevirtual java/lang/StringBuilder/append(I)Ljava/lang/StringBuilder;

ldc ” is available.”

invokevirtual

java/lang/StringBuilder/append(Ljava/lang/String;)Ljava/lang/StringBuilder;

invokevirtual java/lang/StringBuilder/toString()Ljava/lang/String;

invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V

iconst_0

invokestatic java/lang/System/exit(I)V

goto 32

aload_0

dup

getfield Hotel/roomNum I

iconst_1

iadd

putfield Hotel/roomNum I

aload_0

getfield Hotel/roomNum I

bipush 100

if_icmplt 5

getstatic java/lang/System/out Ljava/io/PrintStream;

ldc ”No vacancy”

invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V

return

The instructions in Listing 2-2 aren’t Java source code instructions. They’re Java bytecode instructions. When you write a Java program, you write source code instructions (like the instructions in Listing 2-1). After writing the source code, you run a program (that is, you apply a tool) to your source code. The program is a compiler. The compiler translates your source code instructions into Java bytecode instructions. In other words, the compiler takes code that you can write and understand (like the code in Listing 2-1) and translates your code into code that a computer can execute (like the code in Listing 2-2).

technicalstuff.eps You might put your source code in a file named Hotel.java. If so, the compiler probably puts the Java bytecode in another file named Hotel.class. Normally, you don’t bother looking at the bytecode in the Hotel.class file. In fact, the compiler doesn’t encode the Hotel.class file as ordinary text, so you can’t examine the bytecode with an ordinary editor. If you try to open Hotel.class with Notepad, TextEdit, KWrite, or even Microsoft Word, you’ll see nothing but dots, squiggles, and other gobbledygook. To create Listing 2-2, I had to apply yet another tool to my Hotel.class file. That tool displays a text-like version of a Java bytecode file. I used Ando Saabas’s Java Bytecode Editor (www.cs.ioc.ee/~ando/jbe).

remember.eps No one (except for a few crazy developers in some isolated labs in faraway places) writes Java bytecode. You run software (a compiler) to create Java bytecode. The only reason to look at Listing 2-2 is to understand what a hard worker your computer is.

What is a Java virtual machine?

“A Java virtual machine runs your code (and other peoples’ Java code) on your computer.”

–Barry Burd, Java For Dummies, 5th Edition

In the earlier “What is a compiler?” section, I make a big fuss about computers following instructions like the ones in Listing 2-2. As fusses go, it’s a very nice fuss. But if you don’t read every fussy word, you may be misguided. The exact wording is “. . . computers follow cryptic instructions like the ones in Listing 2-2.” The instructions in Listing 2-2 are a lot like instructions that a computer can execute, but generally, computers don’t execute Java bytecode instructions. Instead, each kind of computer processor has its own set of executable instructions, and each computer operating system uses the processor’s instructions in a slightly different way.

Here’s a hypothetical situation: Imagine that you run the Linux operating system on a computer that has an old Pentium processor. Your friend runs Linux on a computer with a different kind of processor — a PowerPC processor. (In the 1990s, Intel Corporation made Pentium processors, and IBM made PowerPC processors.)

Listing 2-3 contains a set of instructions to display Hello world! on the computer screen.* The instructions work on a Pentium processor running the Linux operating system.

Listing 2-3: A Simple Program for a Pentium Processor

.data

msg:

.ascii “Hello, world! ”

len = . - msg

.text

.global _start

_start:

movl $len,%edx

movl $msg,%ecx

movl $1,%ebx

movl $4,%eax

int $0x80

movl $0,%ebx

movl $1,%eax

int $0x80

Listing 2-4 contains another set of instructions to display Hello world! on the screen.** The instructions in Listing 2-4 work on a PowerPC processor running Linux.

Listing 2-4: A Simple Program for a PowerPC Processor

.data

msg:

.string “Hello, world! ”

len = . - msg

.text

.global _start

_start:

li 0,4

li 3,1

lis 4,msg@ha

addi 4,4,msg@l

li 5,len

sc

li 0,1

li 3,1

sc

The instructions in Listing 2-3 run smoothly on a Pentium processor. But these instructions mean nothing to a PowerPC processor. Likewise, the instructions in Listing 2-4 run nicely on a PowerPC, but these same instructions are complete gibberish to a computer with a Pentium processor. So your friend’s PowerPC software might not be available on your computer. And your Intel computer’s software might not run at all on your friend’s computer.

Now go to your cousin’s house. Your cousin’s computer has a Pentium processor (just like yours), but your cousin’s computer runs Windows instead of Linux. What does your cousin’s computer do when you feed it the Pentium code in Listing 2-3? It screams, “Not a valid Win32 application” or “Windows can’t open this file.” What a mess!

Java bytecode creates order from all this chaos. Java bytecode is something like the code in Listings 2-3 and 2-4, but Java bytecode isn’t specific to one kind of processor or to one operating system. Instead, a set of Java bytecode instructions runs on any computer. If you write a Java program, and compile that Java program into bytecode, then your computer can run the bytecode, your friend’s computer can run the bytecode, your grandmother’s supercomputer can run the bytecode, and with any luck, your tiny, little cellphone can run the bytecode.

cross-reference.eps For a look at some Java bytecode, see Listing 2-2. But remember, you never have to write or decipher Java bytecode. Writing bytecode is the compiler’s job. Deciphering bytecode is the Java virtual machine’s job.

With Java, you can take a bytecode file that you created with a Windows computer, copy the bytecode to who-knows-what kind of computer, and then run the bytecode with no trouble at all. That’s one of the many reasons why Java has become popular so quickly. This outstanding feature, which gives you the ability to run code on many different kinds of computers, is called portability.

What makes Java bytecode so versatile? This fantastic universality enjoyed by Java bytecode programs comes from the Java virtual machine. The Java virtual machine is one of those three tools that you must have on your computer.

Imagine that you’re the Windows representative to the United Nations Security Council. (See Figure 2-1.) The Macintosh representative is seated to your right, and the Linux representative is on your left. (Naturally, you don’t get along with either of these people. You’re always cordial to one another, but you’re never sincere. What do you expect? It’s politics!) The distinguished representative from Java is at the podium. The Java representative is speaking in bytecode, and neither you nor your fellow ambassadors (Mac and Linux) understand a word of Java bytecode.

Figure 2-1: An imaginary meeting of the UN Security Council.

9781118128329 fg0201.eps

But each of you has an interpreter. Your interpreter translates from bytecode to Windows while the Java representative speaks. Another interpreter translates from bytecode to Macintosh-ese. And a third interpreter translates bytecode into Linux-speak.

Think of your interpreter as a virtual ambassador. The interpreter doesn’t really represent your country, but the interpreter performs one of the important tasks that a real ambassador performs. The interpreter listens to bytecode on your behalf. The interpreter does what you would do if your native language was Java bytecode. The interpreter pretends to be the Windows ambassador, and sits through the boring bytecode speech, taking in every word, and processing each word in some way or other.

You have an interpreter — a virtual ambassador. In the same way, a Windows computer runs its own bytecode interpreting software. That software is the Java virtual machine.

A Java virtual machine is a proxy, an errand boy, a go-between. The JVM serves as an interpreter between Java’s run-anywhere bytecode and your computer’s own system. While it runs, the JVM walks your computer through the execution of bytecode instructions. The JVM examines your bytecode, bit by bit, and carries out the instructions described in the bytecode. The JVM interprets bytecode for your Windows system, your Mac, or your Linux box, or for whatever kind of computer you’re using. That’s a good thing. It’s what makes Java programs more portable than programs in any other language.

Developing Software

“All this has happened before, and all this will happen again.”

–Battlestar Galactica, 2003-2009, NBC Universal

When you create a Java program, you repeat the same steps over and over again. Figure 2-2 illustrates the cycle.

Figure 2-2: Developing a Java program.

9781118128329 fg0202.eps

First, you write a program. After writing the first draft, you repeatedly compile, run, and modify the program. With a little experience, the compile and run steps become very easy. In many cases, one mouse click starts the compilation or the run.

However, writing the first draft and modifying the code are not one-click tasks. Developing code requires time and concentration.

remember.eps Never be discouraged when the first draft of your code doesn’t work. For that matter, never be discouraged when the twenty-fifth draft of your code doesn’t work. Rewriting code is one of the most important things you can do (aside from ensuring world peace).

ontheweb.eps For detailed instructions on compiling and running Java programs, visit this book’s website.

When people talk about writing programs, they use the wording in Figure 2-2. They say, “You compile the code” and “You run the code.” But the “you” isn’t always accurate, and the “code” differs slightly from one part of the cycle to the next. Figure 2-3 describes the cycle from Figure 2-2 in a bit more detail.

tip.eps For most people’s needs, Figure 2-3 contains too much information. If I click a Run icon, I don’t have to remember that the computer runs code on my behalf. And for all I care, the computer can run my original Java code or some bytecode knock-off of my original Java code. The details in Figure 2-3 aren’t important. The only use for Figure 2-3 is to help you if the loose wording in Figure 2-2 confuses you. If Figure 2-2 doesn’t confuse you, then ignore Figure 2-3.

Figure 2-3: Who does what with which code?

9781118128329 fg0203.eps

What is an Integrated Development Environment?

“An integrated development environment helps you manage your Java code and provides convenient ways for you to write, compile, and run your code.”

–Barry Burd, Java For Dummies, 5th Edition

In the olden days, writing and running a Java program involved opening several windows — a window for typing the program, another window for running the program, and maybe a third window to keep track of all the code that you’ve written. (See Figure 2-4.)

An integrated development environment seamlessly combines all this functionality into one well-organized application. (See Figure 2-5.)

Java has its share of integrated development environments. Some of the more popular products include Eclipse, IntelliJ IDEA, and NetBeans. Some fancy environments even have drag-and-drop components so that you can design your graphical interface visually. (See Figure 2-6.)

Figure 2-4: Developing code without an integrated development environment.

9781118128329 fg0204.eps

Figure 2-5: Developing code with the Eclipse integrated development environment.

9781118128329 fg0205.eps

Figure 2-6: Using the drag-and-drop Swing GUI Builder in the NetBeans IDE.

9781118128329 fg0206.tif

To run a program, you might click a toolbar button or choose Run from a menu. To compile a program, you might not have to do anything at all. (You might not even have to issue a command. Some IDEs compile your code automatically while you type it.)

ontheweb.eps For help with installing and using an integrated development environment, see this book’s website.

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

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