Who's Afraid of Java?

Who's Afraid of Java? - the WWW version

ISBN: 0-12-339101-6

Copyright 1997, 1996 by AP Professional

Copyright 2000, 1997, 1996 by Chrysalis Software Corporation


Return to the table of contents


Return to my main page


Hardware Fundamentals

Getting Started

Like any complex tool, the computer can be understood on several levels. For example, it's entirely possible to learn to drive an automobile without having the slightest idea of how it works. The analogy with computers is that it's relatively easy to learn how to use a word processor without having any notion of how such programs work. On the other hand, programming is much more closely analogous to designing an automobile than it is to driving one; therefore, we're going to have to go into some detail about the internal workings of a computer, not at the level of electronic components, but at the lowest level important to a Java programmer. This is a book on learning to program in Java, not on how a computer works. Therefore, it might seem better to start there and eliminate this detour, and indeed many (perhaps most) books on Java do exactly that. However, in working out in detail how I'm going to explain Java to you, I've come to the conclusion that it would be virtually impossible to explain why certain features of the language exist and how they actually work, without your understanding how they relate to the underlying computer hardware.

I haven't come to this position by pure logical deduction, either. In fact, I've worked backward from the concepts that you will need to know to program in Java to the specific underlying information that you will have to understand first. I'm thinking in particular of one specific concept, the reference, which is supposed to be extremely difficult for a beginning programmer in Java to grasp. With the approach we're taking, you shouldn't have much trouble understanding this concept by the time you get to it in Chapter inventor.htm; it's noted as such in the discussion there. I'd be interested to know how you find my explanation there, given the background that you'll have by that point; don't hesitate to e-mail me about this topic (or any other, for that matter).

On the other hand, if you're an experienced programmer, a lot of this will be just review for you. Nonetheless, it can't hurt to go over the basics one more time before diving into the ideas and techniques that make Java different from other languages.

Now let's begin with some definitions and objectives for this chapter.

Definitions

A digit is one of the characters used in any positional number system to represent all numbers starting at 0 and ending at one less than the base of the number system. In the decimal system, there are ten digits, 0-9, and in the hexadecimal system there are sixteen digits, 0-9 and a-f.

A binary number system is one that uses only two digits, 0 and 1.

A hexadecimal number system is one that uses 16 digits, 0-9 and a-f.

A variable is a programming construct that represents a specific item of data that we wish to keep track of in a program. Some examples are the weight of a pumpkin or the number of cartons of milk in the inventory of a store.

Objectives for This Chapter

By the end of this chapter, you should:

  1. Understand the programmer's view of the most important pieces of hardware in your computer.
  2. Understand the programmer's view of the most important pieces of software in your computer.
  3. Be able to solve simple problems using both the binary and hexadecimal number systems.
  4. Understand how whole numbers are stored in the computer.

Behind the Curtain

First we'll need to expand on the definition of hardware. As noted earlier, hardware means the physical components of a computer, the ones you can touch.1 Examples are the monitor, the keyboard, the printer, and all of the interesting electronic and electromechanical components inside the case of your computer.2 Right now we're concerned with the Java programmer's view of the hardware. The hardware components of a computer with which you'll be primarily concerned are the disk, RAM (short for Random Access Memory), and the CPU (short for Central Processing Unit).3 We'll take up each of these topics in turn.

Disk

When you sit down at your computer in the morning, before you turn it on, where are the programs you're going to run? To make this more specific, suppose you're going to use a word processor to revise a letter you wrote yesterday before you turned the computer off. Where is the letter, and where is the word processing program?

You probably know the answer to this question: They are stored on a disk inside the case of your computer.4 Disks use magnetic recording media, much like the material used to record speech and music on cassette tapes, to store information in a way that will not be lost when the power is turned off. How exactly is this information (which may be either executable programs or data such as word processing documents) stored?

We don't have to go into excruciating detail on the storage mechanism, but it is important to understand some of its characteristics. A disk consists of one or more circular platters, which are extremely flat and smooth pieces of metal or glass covered with a material that can be very rapidly and accurately magnetized in either of two directions, "north" and "south". To store large amounts of data, each platter is divided into many millions of small regions, each of which can be magnetized in either direction independent of the other regions. The magnetization is detected and modified by recording heads, similar in principle to those used in tape cassette decks. However, in contrast to the cassette heads, which make contact with the tape while they are recording or playing back music or speech, the disk heads "fly" a few millionths of an inch away from the platters, which rotate at very high velocity.5 The separately magnetizable regions used to store information are arranged in groups called sectors, which are in turn arranged in concentric circles called tracks. All tracks on one side of a given platter (a recording surface) can be accessed by a recording head dedicated to that recording surface; each sector is used to store some number of bytes of the data, generally a few hundred to a few thousand. "Byte" is a coined word meaning a group of 8 binary digits, or bits for short.6 You may wonder why the data aren't stored in the more familiar decimal system, which of course uses the digits from 0 through 9. This is not an arbitrary decision; on the contrary, there are a couple of very good reasons that data on a disk are stored using the binary system, in which each digit has only two possible states, 0 and 1. One of these reasons is that it's a lot easier to determine reliably whether a particular area on a disk is magnetized "north" or "south" than it is to determine 1 of 10 possible levels of magnetization. Another reason is that the binary system is also the natural system for data storage using electronic circuitry, which is used to store data in the rest of the computer.

Although magnetic storage devices have been around in one form or another since the very early days of computing, the advances in technology just in the last dozen years have been staggering. To comprehend just how large these advances have been, we need to define the term used to describe storage capacities: the Megabyte. The standard engineering meaning of Mega is "multiply by one million", which would make a Megabyte equal to one million (1,000,000) bytes. As we have just seen, however, the natural number system in the computer field is binary. Therefore, "one Megabyte" is often used instead to specify the nearest "round" number in the binary system, which is 2^20 (2 to the 20th power), or 1,048,576 bytes.7 This wasn't obvious to Susan, so I explained it some more, as you can see here:

Susan: Just how important is it to really understand that the Megabyte is 2^20 (1,048,576) bytes? I know that a meg is not really a meg; that is, it's more than a million. But I don't understand 2^20, so is it enough to just take your word on this and not get bogged down as to why I didn't go any further than plane geometry in high school? You see, it makes me worry and upsets me that I don't understand how you "round" a binary number.

Steve: The ^ symbol is a common way of saying "to the power of", so 2^20 would be 2 to the power of 20; that is, twenty 2s multiplied together. This is a "round" number in binary just as 10 * 10 * 10 (1000) is a "round" number in decimal.

1985, a Space Odyssey

With that detail out of the way, we can see just how far we've come in a short period of time. In 1985, I purchased a 20 Megabyte disk for $900 ($45 per Megabyte); its access time, which measures how long it takes to retrieve data, was approximately 100 milliseconds (milli = 1/1000, so a millisecond is one thousandth of a second). In April 1997, a 6510 Megabyte disk cost as little as $449, or approximately 7 cents per Megabyte; in addition to delivering 650 times as much storage per dollar, this disk had an access time of 14 milliseconds, which is approximately 7 times as fast as the old disk. Of course, this significantly understates the amount of progress in technology in both economic and technical terms. For one thing, a 1997 dollar is worth considerably less than a 1985 dollar. In addition, the new drive is superior in every other measure as well: It is much smaller than the old one, consumes much less power, and has many times the projected reliability of the old drive.

This tremendous increase in performance and price has prevented the long-predicted demise of disk drives in favor of new technology. However, the inherent speed limitations of disks still require us to restrict their role to the storage and retrieval of data for which we can afford to wait a relatively long time.

You see, while 14 milliseconds isn't very long by human standards, it is a long time indeed to a modern computer. This will become more evident as we examine the next essential component of the computer, the RAM.

RAM

The working storage of the computer, where data and programs are stored while we're using them, is called RAM, which is an acronym for Random Access Memory.8 For example, your word processor is stored in RAM while you're using it. The document you're working on is likely to be there as well unless it's too large to fit all at once, in which case parts of it will be retrieved from the disk as needed. Since we have already seen that both the word processor and the document are stored on the disk in the first place, why not leave them there and use them in place, rather than copying them into RAM?

The answer, in a word, is speed. RAM is physically composed of millions of microscopic switches on a small piece of silicon known as a chip: a 4 Megabit RAM chip has approximately four million of them.9 Each of these switches can be either on or off; we consider a switch that is "on" to be storing a 1, and a switch that is "off" to be storing a 0. Just as in storing information on a disk, where it was easier to magnetize a region in either of two directions, it's a lot easier to make a switch that can be turned on or off reliably and quickly than one that can be set to any value from 0 to 9 reliably and quickly. This is particularly important when you're manufacturing millions of them on a silicon chip the size of your fingernail.

A main difference between disk and RAM is what steps are needed to access different areas of storage. In the case of the disk, the head has to be moved to the right track (an operation known as a seek), and then we have to wait for the platter to spin so that the region we want to access is under the head (called rotational delay). On the other hand, with RAM, the entire process is electronic; we can read or write any byte immediately, as long as we know which byte we want. To specify a given byte, we have to supply a unique number called its memory address or just address for short.

Return to Sender, Address Unknown

What is an address good for? Let's see how my discussion with Susan on this topic started.

Susan: About memory addresses: Are you saying that each little itty bitty tiny byte of RAM is a separate address? Well, this is a little hard to imagine.

Steve: Actually, each byte of RAM has a separate address, which doesn't change, and a value, which does.

In case the notion of an address of a byte of memory on a piece of silicon is too abstract, it might help to think of an address as a set of directions as to how to find the byte being addressed, much like directions to someone's house. For example, "Go three streets down, then turn left. It's the second house on the right". With such directions, the house number wouldn't need to be written on the house. Similarly, the memory storage areas in RAM are addressed by position; you can think of the address as telling the hardware which street and house you want, by giving directions similar in concept to the preceding example. Therefore, it's not necessary to encode the addresses into the RAM explicitly.

Susan wanted a better picture of this somewhat abstract idea.

Susan: Where are the bytes on the RAM, and what do they look like?

Steve: Each byte corresponds to a microscopic region of the RAM chip. As to what they look like, have you ever seen a printed circuit board such as the ones inside your computer? Imagine the lines on that circuit board reduced thousands of times in size to microscopic dimensions, and you'll have an idea of what a RAM chip looks like inside.

Since it has no moving parts, storing and retrieving data in RAM is much faster than waiting for the mechanical motion of a disk platter turning.10 As we've just seen, disk access times are measured in milliseconds, or thousandths of a second. RAM access times, however, are measured in nanoseconds (abbreviated ns); nano means one billionth. In early 1997, a typical speed for RAM was 70 ns, which means that it is possible to read a given data item from RAM about 200,000 times as quickly as from a disk with an access time of 14 milliseconds. In that case, why not use disks only for permanent storage, and read everything into RAM in the morning when we turn on the machine?

The reason is cost. In early 1997, the cost of 16 Megabytes of RAM was approximately $80. For that same amount of money, you could have bought over 1100 Megabytes of disk space!11! Therefore, we must reserve RAM for tasks where speed is all-important, such as running your word processing program and holding a letter while you're working on it. Also, since RAM is an electronic storage medium (rather than a magnetic one), it does not maintain its contents when the power is turned off. This means that if you had a power failure while working with data only in RAM, you would lose everything you had been doing.12 This is not merely a theoretical problem, by the way; if you don't remember to save what you're doing in your word processor once in a while, you might lose a whole day's work from a power outage of a few seconds.13 Before we get to how a program actually works, we need to develop a better picture of how RAM is used. As I've mentioned before, you can think of RAM as consisting of a large number of bytes, each of which has a unique identifier called an address. This address can be used to specify which byte we mean, so the program might specify that it wants to read the value in byte 148257, or change the value in byte 66666. Susan wanted to make sure she had the correct understanding of this topic.

Susan: Are the values changed in RAM depending on what program is loaded in it?

Steve: Yes, and they also change while the program is executing. RAM is used to store both the program itself and the values it manipulates.

This is all very well, but it doesn't answer the question of how we actually use the data in RAM. The answer is by means of variables, which represent specific items of data that we wish to keep track of in our programs, such as weights and numbers of items.

You can put something in a variable, and it will stay there until you store something else there; you can also look at it to find out what's in it. As you might expect, several types of variables are used to hold different kinds of data; the first ones we will look at are variables representing whole numbers (the so-called integer variables), which are a subset of the category called numeric variables. As this suggests, there are also variables that represent numbers that can have fractional parts. We'll look at these so-called floating-point variables briefly in a later chapter.

Different types of variables require different amounts of RAM to store them, depending on the amount of data they contain; a very common type of numeric variable, known as a short, requires 16 bits (that is, 2 bytes) of RAM to hold any of 65536 different values, from -32768 to 32767, including 0. These odd-looking numbers are the result of using the binary system. To make this number system more intelligible, I have written the following little fable.

Odometer Trouble

Once upon a time, the Acme company had a factory that made golf carts. One day, Bob, the president of Acme, decided to add an odometer to the carts, so that the purchaser of the cart could estimate when to recharge the battery. To save money, Bob decided to buy the little numbered wheels for the odometers and have his employees put the odometers together. The minimum order was a thousand odometer wheels, which was more than he needed for his initial run of 50 odometers. When he got the wheels, however, he noticed that they were defective: Instead of the numbers 0-9, each wheel had only two numbers, 0 and 1. Of course, he was quite irritated by this error, and attempted to contact the company from which he had purchased the wheels, but it had closed down for a month for summer vacation. What was he to do until it reopened?

While he was fretting about this problem, the employee who had been assigned to the task of putting the odometers together from the wheels came up with a possible solution. This employee, Jim, came into Bob's office and said, "Bob, I have an idea. Since we have lots of orders for these odometer-equipped carts, maybe we can make an odometer with these funny wheels and tell the customers how to read the numbers on the odometer."

Bob was taken aback by this idea. "What do you mean, Jim? How can anyone read those screwy odometers?"

Jim had given this some thought. "Let's take a look at what one of these odometers, say with five wheels, can display. Obviously, it would start out reading 00000, just like a normal odometer. Then when one mile has elapsed, the right-most wheel turns to 1, so the whole display is 00001; again, this is no different from a normal odometer."

"Now we come to the tricky part. The right-most wheel goes back to 0, not having any more numbers to display, and pushes the `tens' wheel to 1; the whole number now reads 00010. Obviously, one more mile makes it 00011, which gives us the situation shown in the following diagram:

  Normal odometer	Funny odometer

00000 00000

00001 00001

00002 00010

00003 00011

The first few numbers (Figure firstfew)

Jim continued, "What's next? This time, the right-most wheel turns over again to 0, triggering the second wheel to its next position. At this point, however, the second wheel is already at its highest value, 1; therefore, it also turns over to 0 and increments the third wheel. It's not hard to follow this for a few more miles, as illustrated in Figure nextfew.

  Normal odometer	Funny odometer

00004 00100

00005 00101

00006 00110

00007 00111

The next few numbers (Figure nextfew)

Bob said, "I get it. It's almost as though we were counting normally, except that you skip all the numbers that have anything but 0s or 1s in them."

"That's right, Bob. So I suppose we could make up a list of the `real' numbers and give it to the customers to use until we can replace these odometers with normal ones. Perhaps they'll be willing to work with us on this problem."

"Okay, Jim, if you think they'll buy it. Let's get a few of the customers we know the best and ask them if they'll try it; we won't charge them for the odometers until we have the real ones, but maybe they'll stick with us until then. Perhaps any odometer would be better than no odometer at all."

Jim went to work, making some odometers out of the defective wheels; however, he soon figured out that he had to use more than five wheels, because that allowed only numbers from 0 to 31. How did he know this?

Each wheel has two numbers, 0 and 1. So with one wheel, we have a total of two combinations. Two wheels can have either a 0 or a 1 for the first number, and the same for the second number, for a total of four combinations. With three wheels, the same analysis holds: 2 numbers for the first wheel * 2 for the second wheel * 2 for the third wheel = 8 possibilities in all; actually, they are the same 8 possibilities we saw in Figures firstfew and nextfew.

A pattern is beginning to develop: For each added wheel, we get twice as many possible combinations. To see how this continues, take a look at Figure howmany, which shows the count of combinations versus the number of wheels for all wheel counts up to 16 (i.e., 16-bit quantities).

Number of wheels	Number of combinations14

1 2

2 4

3 8

4 16

5 32

6 64

7 128

8 256

9 512

10 1024

11 2048

12 4096

13 8192

14 16384

15 32768

16 65536

How many combinations? (Figure howmany)

Jim decided that 14 wheels would do the job, since the life span of the golf cart probably wouldn't exceed 16,383 miles, and so he made up the odometers. The selected customers turned out to be agreeable and soon found that having even a weird odometer was better than none, especially since they didn't have to pay for it. However, one customer did have a complaint: The numbers on the wheels didn't seem to make sense when translated with the chart supplied by Acme. The customer estimated that he had driven the cart about 9 miles, but the odometer displayed the following number:

11111111110111

which, according to his translation chart, was 16,375 miles. What could have gone wrong?

Jim decided to have the cart brought in for a checkup, and what he discovered was that the odometer cable had been hooked up backwards. That is, instead of turning the wheels forward, they were going backwards. That was part of the solution, but why was the value 16375? Just like a car odometer, in which 99999 (or 999999, if you have a six-wheel odometer) is followed by 0, going backwards from 0 reverses that progression. Similarly, the number 11111111111111 on the funny odometers would be followed by 00000000000000, since the "carry" off the left-most digit is lost. Therefore, if you start out at 0 and go backward one mile, you'll get

11111111111111

The next mile will turn the last digit back to 0, producing

11111111111110

What happens next? The last wheel turns back to 1, and triggers the second wheel to switch as well:

11111111111101

The next few "backward" numbers look like this:

11111111111100

11111111111011

11111111111010

11111111111001

11111111111000

11111111110111

and so on. If you look at the right-hand end of these numbers, you'll see that the progression is just the opposite of the "forward" numbers.

As for the customer's actual mileage, the last one of these is the number the customer saw on his backward odometer. Apparently, he was right about the distance driven, since this is the ninth "backward" number. So Jim fixed the backward odometer cable and reset the value to the correct number, 00000000001001, or nine miles.

Eventually, Acme got the right odometer wheels with 0-9 on them, replaced the peculiar ones, and everyone lived happily ever after.

THE END

Back to the Future

Of course, the wheels that made up the funny odometers contain only two digits, 0 and 1, so the odometers use the binary system for counting. Now it should be obvious why we will see numbers like 65536 and 32768 in our discussions of the number of possible different values that a variable can hold: Variables are stored in RAM as collections of bytes, each of which contains 8 bits. As the list of combinations indicates, 8 bits (1 byte) provide 256 different combinations, while 16 bits (2 bytes) can represent 65,536 different possible values.

But what about the "backward" numbers with a lot of 1s on the left? As the fable suggests, they correspond to "negative" numbers. That is, if moving two miles forward from 0 registers as 00000000000010, and moving two miles backward from 0 registers as 11111111111110, then the latter number is in some sense equivalent to -2 miles. In Java, all integer variables can store either positive or negative values, and this "backward" representation is the way that negative integers are represented in a Java variable.

Over-Hexed

You may have noticed that it's tedious and error prone to represent numbers in binary; a long string of 0s and 1s is hard to remember or to copy. For this reason, the pure binary system is hardly ever used to specify numbers in computing. However, we have already seen that binary is much more "natural" for computers than the more familiar decimal system. Is there a number system that we humans can use a little more easily than binary, while retaining the advantages of binary for describing internal events in the computer?

As it happens, there is. It's called hexadecimal, which means "base 16". As a rule, the term hexadecimal is abbreviated to hex. Since there are 16 possible combinations of 4 bits (2*2*2*2), hexadecimal notation allows 4 bits of a binary number to be represented by one hex digit. Unfortunately, however, there are only 10 "normal" digits, 0-9.15 To represent a number in any base, you need as many different digit values as the base, so that any number less than the base can be represented by one digit. For example, in base 2, you need only two digits, 0 and 1. In base 8 (octal), you need eight digits, 0-7.16 So far, so good. But what about base 16? To use this base, we need 16 digits. Since only 10 numeric digits are available, hex notation needs a source for the other six digits. Because letters of the alphabet are available and familiar, the first six letters, a-f, were adopted for this service.17 Although the notion of a base 16 number system doesn't seem strange to people who are familiar with it, it can really throw someone who learned normal decimal arithmetic solely by rote, without understanding the concepts on which it is based. This topic of hexadecimal notation occupied Susan and me for quite awhile; here's some of the discussion we had about it:

Susan: I don't get this at all! What is the deal with the letters in the hex system? I guess it would be okay if 16 wasn't represented by 10!

Steve: Well, there are only 10 "normal" digits, 0-9. To represent a number in any base, you need as many "digits" as the base, so that any number less than the base can be represented by one "digit". This is no problem with a base less than ten, such as octal, but what about base 16? To use this base we need 16 digits, 0-9 and a-f. One way to remember this is to imagine that the "hex" in "hexadecimal" stands for the six letters a-f and the "decimal" stands for the 10 digits 0-9.

Susan: OK, so a hex digit represents 16 bits? So then is hex equal to 2 bytes? According to the preceding, a hex digit is 4 bits.

Steve: Yes, a hex digit represents 4 bits. Let's try a new approach. First, let me define a new term, a hexit. That's short for "hex digit", just like "bit" is short for "binary digit". Now let's look at the answers to the following questions.

  1. How many numbers can be represented with no more than one decimal digit?
  2. How many numbers can be represented with no more than two decimal digits?
  3. How many numbers can be represented with no more than three decimal digits?
  4. How many numbers can be represented with no more than four decimal digits?
  5. How many numbers can be represented with no more than one bit?
  6. How many numbers can be represented with no more than two bits?
  7. How many numbers can be represented with no more than three bits?
  8. How many numbers can be represented with no more than four bits?
  9. How many numbers can be represented with no more than one hexit?
  10. How many numbers can be represented with no more than two hexits?
  11. How many numbers can be represented with no more than three hexits?
  12. How many numbers can be represented with no more than four hexits?

The answers are:

  1. 10
  2. 100
  3. 1000
  4. 10000
  5. 2
  6. 4
  7. 8
  8. 16
  9. 16
  10. 256
  11. 4096
  12. 65536

What do all these answers have in common? Let's look at the answers a little differently, in powers of 10, 2, and 16, respectively:

  1. 10 = 10^1
  2. 100 = 10^2
  3. 1000 = 10^3
  4. 10000 = 10^4
  5. 2 = 2^1
  6. 4 = 2^2
  7. 8 = 2^3
  8. 16 = 2^4
  9. 16 = 16^1
  10. 256 = 16^2
  11. 4096 = 16^3
  12. 65536 = 16^4

That is, a number that has one digit can represent "base" different values, where "base" is two, ten, or sixteen (in our examples). Every time we increase the size of the number by one more digit, we can represent "base" times as many possible different values, or in other words, we multiply the range of values that the number can represent by the base. Thus, a two-digit number can represent any of "base*base" values, a three-digit number can represent any of "base*base*base" values, and so on. That's the way positional number systems such as decimal, binary, and hex work. If you need a bigger number, you just add more digits.

Okay, so what does this have to do with hex? If you look at the above table, you'll see that 2^4 (16) is equal to 16^1. That means that 4 bits are exactly equivalent to one hexit in their ability to represent different numbers: Exactly 16 possible numbers can be represented by four bits, and exactly 16 possible numbers can be represented by one hexit.

This means that you can write one hexit wherever you would otherwise have to use four bits, as illustrated in Figure binhex.

4-bit value  1-hexit value


0000 0

0001 1

0010 2

0011 3

0100 4

0101 5

0110 6

0111 7

1000 8

1001 9

1010 a

1011 b

1100 c

1101 d

1110 e

1111 f

Binary to hex conversion table (Figure binhex)

So an 8-bit number, such as:

0101 1011

can be translated directly into a hex value, like this:

5 b

For this reason, binary is almost never used. Instead, we use hex as a shortcut to eliminate the necessity of reading, writing, and remembering long strings of bits.

Susan: A hex digit or hexit is like a four-wheel odometer in binary. Since each wheel is capable of only one of two values, being either (1) or (0), then the total number of possible values is 16. Thus your 2*2*2*2 = 16. I think I've got this down.

Steve: You certainly do!

Susan: If it has 4 bits and you have 2 of them, then won't there be eight "wheels" and so forth? So 2 hex would hold XXXXXXXX places and 3 hex would hold XXXXXXXXXXXX places.

Steve: Correct. A one-hexit number is analogous to a one-digit decimal number. A one-hexit number contains 4 bits and therefore can represent any of 16 values. A two-hexit number contains 8 bits and therefore can represent any of 256 values.

Now that we've seen how each hex digit corresponds exactly to a group of four binary digits, here's an exercise you can use to improve your understanding of this topic: Invent a random string of four binary digits and see where it is in Figure binhex. I guarantee it'll be there somewhere! Then look at the "hex" column and see what "digit" it corresponds to. There's nothing really mysterious about hex; since we have run out of digits after 9, we have to use letters to represent the numbers `ten', `eleven', `twelve', `thirteen', `fourteen', and `fifteen'.

Here's a table showing the correspondence between some decimal, hex, and binary numbers, with the values of each digit position in each number base indicated, and the calculation of the total of all of the bit values in the binary representation, as shown in Figure diffrep.

  Decimal      Hexadecimal     Binary           Sum of Binary

Place Values Place Values Place Values Digit Values

10 1 16 1 16 8 4 2 1


0 0 0 0 0 0 0 0 = 0 + 0 + 0 + 0 + 0

1 0 1 0 0 0 0 1 = 0 + 0 + 0 + 0 + 1

2 0 2 0 0 0 1 0 = 0 + 0 + 0 + 2 + 0

3 0 3 0 0 0 1 1 = 0 + 0 + 0 + 2 + 1

4 0 4 0 0 1 0 0 = 0 + 0 + 4 + 0 + 0

5 0 5 0 0 1 0 1 = 0 + 0 + 4 + 0 + 1

6 0 6 0 0 1 1 0 = 0 + 0 + 4 + 2 + 0

7 0 7 0 0 1 1 1 = 0 + 0 + 4 + 2 + 1

8 0 8 0 1 0 0 0 = 0 + 8 + 0 + 0 + 0

9 0 9 0 1 0 0 1 = 0 + 8 + 0 + 0 + 1

1 0 0 a 0 1 0 1 0 = 0 + 8 + 0 + 2 + 0

1 1 0 b 0 1 0 1 1 = 0 + 8 + 0 + 2 + 1

1 2 0 c 0 1 1 0 0 = 0 + 8 + 4 + 0 + 0

1 3 0 d 0 1 1 0 1 = 0 + 8 + 4 + 0 + 1

1 4 0 e 0 1 1 1 0 = 0 + 8 + 4 + 2 + 0

1 5 0 f 0 1 1 1 1 = 0 + 8 + 4 + 2 + 1

1 6 1 0 1 0 0 0 0 = 16 + 0 + 0 + 0 + 0

1 7 1 1 1 0 0 0 1 = 16 + 0 + 0 + 0 + 1

1 8 1 2 1 0 0 1 0 = 16 + 0 + 0 + 2 + 0

1 9 1 3 1 0 0 1 1 = 16 + 0 + 0 + 2 + 1

Different representations of the same numbers (Figure diffrep)

Another reason to use hex rather than decimal is that byte values expressed as hex digits can be combined directly to produce larger values, which is not true with decimal digits. In case this isn't obvious, let's go over it in more detail. Since each hex digit (0-f) represents exactly 4 bits, two of them (00-ff) represent 8 bits, or one byte. Similarly, 4 hex digits (0000-ffff) represent 16 bits, or a short value; the first two digits represent the first byte of the 2-byte value, and the last two digits, the second byte. This can be extended to any number of bytes. On the other hand, representing 4 bits requires two decimal digits, as the values range from 00-15, whereas it takes three digits (000-255) to represent one byte. A 2-byte value requires five decimal digits, since the value can be from 00000 to 65535. As you can see, there's no simple relationship between the decimal digits representing each byte and the decimal representation of a 2-byte value.

Susan had some more thoughts on the hexadecimal number system. Let's listen in.

Susan: I think you need to spend a little more time reviewing the hex system, like an entire chapter.<G> Well, I am getting the impression that we are going to be working with hex, so I am trying to concentrate my understanding on that instead of binary. I think this all moves a little too fast for me. I don't know what your other reviewers are saying but I just feel like I get a definition of a abstract concept, and the next thing I know I am supposed to be doing something with it, like make it work. Ha! I personally need to digest new concepts, I really need to think them over a bit, to take them in and absorb them. I just can't start working with it right away.

As usual, I've complied with her request; the results are immediately ahead.

Exercises

Here are some exercises that you can use to check your understanding of the binary and hexadecimal number systems.18 I've limited the examples to addition and subtraction, as that is all that you're ever likely to have to do in these number systems. These operations are exactly like their equivalents in the decimal system, except that as we have already seen, the hexadecimal system has six extra digits after 9: a, b, c, d, e, and f. We have to take these into account in our calculations: for example, adding 9 and 5, rather than producing 14, produces e.

  1. Using the hexadecimal system, answer these problems:
    1. 1a + 2e = ?
    2. 12 + 18 = ?
    3. 50 - 12 = ?
  2. In the binary system, answer these problems:
    1. 101 + 110 = ?
    2. 111 + 1001 = ?
    3. 1010 - 11 = ?

  1. Let's suppose that x is a short, currently holding the value 32767, or 7fff in hex. What is the result of adding 1 to x, in both decimal and hex?

Answers to exercises can be found at the end of the chapter.

The CPU

There's one point we haven't covered yet: how these variables are actually referred to and modified. The ultimate answer to that question is that every action in the computer is performed by another piece of hardware: the CPU (Central Processing Unit). Like RAM, it is physically composed of millions of microscopic transistors on a chip; however, the organization of these transistors in a CPU is much more complex than on a RAM chip, as the latter's functions are limited to the storage and retrieval of data. The CPU, on the other hand, is capable of performing dozens or hundreds of different fundamental operations called machine instructions, or just instructions for short. While each instruction performs a very simple function, the tremendous power of the computer lies in the fact that the CPU can perform (or execute) tens or hundreds of millions of these instructions per second.19 These instructions fall into a number of categories: instructions that perform arithmetic operations such as adding, subtracting, multiplying, and dividing; instructions that move information from one place to another in RAM; instructions that compare two quantities to help make a determination as to which instructions need to be executed next and instructions that implement that decision; and other, more specialized types of instructions.

With most languages, the programs you write are translated into machine instructions for a specific CPU. However, one of the design goals of Java was to allow the same translated program to work on any machine. To facilitate this task, the process of making your programs runnable works differently in Java: Instead of your programs being translated into a form designed to be executed on a specific CPU, they are translated into a form that can be executed on any CPU with the intervention of a special program called an interpreter. We'll go into how this works in great detail in the next chapter; for now, just keep in mind that Java programs aren't tied to a specific type of computer.

You've just been subjected to a barrage of information on how a computer works. Let's go over it again briefly before continuing.

Review

Two main components of the computer hardware are of most significance to Java programmers: disk and RAM. Both of these store programs and data for use by Java programs.

Computers represent pieces of information (or data) as binary digits, universally referred to as bits. Each bit can have the value 0 or 1. The binary system is used instead of the more familiar decimal system because it is much easier to make devices that can store and retrieve 1 of 2 values than 1 of 10. Bits are grouped into sets of eight, called bytes.

The disk uses magnetic recording heads to store and retrieve groups of a few hundred bytes on rapidly spinning platters in a few milliseconds. The contents of the disk are not lost when the power is turned off, so it is suitable for more or less permanent storage of programs and data.

RAM, which is an acronym for Random Access Memory, is used to hold programs and data while they're in use. It is made of millions of microscopic transistors on a piece of silicon called a chip. Each bit is stored using a few of these transistors. RAM does not retain its contents when power is removed, so it is not good for permanent storage. However, any byte in a RAM chip can be accessed in about 70 nanoseconds (billionths of a second), which is hundreds of thousands of times as fast as accessing a disk. Each byte in a RAM chip can be independently stored and retrieved without affecting other bytes, by providing the unique memory address belonging to the byte you want to access.

The binary system is the most fundamental number system in the computer, but it is not very convenient for people, especially when dealing with large numbers. For this purpose, the hexadecimal number system is considerably more convenient, as the representations of large numbers are much shorter and easier to remember.

Unlike most other computer languages, Java was designed to allow the same program to be run on various kinds of computers, with the intervention of a special kind of program called an interpreter.

Conclusion

In this chapter, we've covered a lot of material on how a computer actually works. As you'll see, this background is essential if you're going to understand what really happens inside a program. In the next chapter, we'll get to the "real thing": how to write a program to make all this hardware do something useful.

Answers to Exercises

  1. Hexadecimal arithmetic
    1. 48
    2. You probably won't be surprised to hear that Susan didn't care much for this answer originally. Here's the discussion on that topic:
    3. Susan: Problem 1a. My answer is 38. Why? My own personal way of thinking: If a = 10, right? and if e = 14 and if 1 * 10 = 10 and if 2 * 14 = 28, then if you add 10 + 28 you get 38. So please inform me how you arrived at 48? I didn't bother with the rest of the problems. If I couldn't get the first one right, then what was the point?
    4. Steve: Here's how you do this problem, with the letter "h" indicating a hex number:
    5. 10h(1 * 16) + ah(10 * 1)
    6. 20h(2 * 16) + eh(14 * 1)
    7. ------------------
    8. 30h(3 * 16) + 18h(24 * 1 = 1 * 16 + 8 * 1)
    9. Carry the 1 from the low digit to the high digit of the answer, to produce:
    10. 40h(4 * 16) + 8h(8 * 1), or 48 hex, which is the answer.

    11. 2a
    12. 3e

  2. Binary arithmetic
    1. 1011
    2. 10000
    3. 111
  3. -32768, or 8000 in hex
  4. In case you got this wrong, you should remember that the range of short values is -32768 to +32767, with 0000h to 7fffh being considered positive, and 8000h to ffffh considered negative.
  5. Footnotes

    1. Whenever I refer to a computer, I mean a modern microcomputer capable of running MS-DOSTM; these are commonly referred to as PCs. Most of the fundamental concepts are the same in other kinds of computers, but the details differ.
    2. Although it's entirely possible to program without ever seeing the inside of a computer, you might want to look in there anyway, just to see what the CPU, RAM chips, disk drives, etc., look like. Some familiarization with the components would give you a head start if you ever want to expand the capacity of your machine.
    3. Other hardware components can be important to programmers of specialized applications; for example, game programmers need extremely fine control on how information is displayed on the monitor. However, we have enough to keep us busy learning how to write general data-handling programs; you can always learn how to write games later, if you're interested in doing so.
    4. Technically, this is a hard disk, to differentiate it from a floppy disk, the removable storage medium often used to distribute software or transfer files from one computer to another. Although at one time many small computers used floppy disks to store data, the tremendous decrease in hard disk prices means that today even the most inexpensive computer stores programs and data on a hard disk.
    5. The heads have to be as close as possible to the platters because the influence of a magnet (called the magnetic field) drops off very rapidly with distance. Thus, the closer the heads are, the more powerful the magnetic field is and the smaller the region that can be used to read and write data reliably. Of course, this leaves open the question of why the heads aren't in contact with the surface; that would certainly solve the problem of being too far away. Unfortunately, this seemingly simple solution would not work at all. There is a name for the contact of heads and disk surface while the disk is spinning: head crash. The friction caused by such an event destroys both the heads and disk surface almost instantly.
    6. In some old machines, bytes sometimes contained more or less than 8 bits, but the 8-bit byte is virtually universal today.
    7. In case you're not familiar with the ^ notation, the number on its right indicates how many copies of the number to the left have to be multiplied together to produce the final result. For example, 2^5 = 2 * 2 * 2 * 2 * 2, whereas 4^3 = 4 * 4 * 4. Of course, I've just introduced another symbol you might not be familiar with: the * is used to indicate multiplication in programming.
    8. RAM is sometimes called "internal storage", as opposed to "external storage", that is, the disk.
    9. Each switch is made of several transistors. Unfortunately, an explanation of how a transistor works would take us too far afield. Consult any good encyclopedia, such as the Encyclopedia Britannica, for this explanation.
    10. There's also another kind of electronic storage, called ROM, for Read-Only Memory; as its name indicates, you can read from it, but you can't write to it. This is used for storing permanent information, such as the program that allows your computer to read a small program from your boot disk; that program, in turn, reads in the rest of the data and programs needed to start up the computer. This process, as you probably know, is called booting the computer. In case you're wondering where that term came from, it's an abbreviation for bootstrapping, which is intended to suggest the notion of pulling yourself up by your bootstraps.

      You may have noticed that the terms RAM and ROM aren't symmetrical; why isn't RAM called RWM, Read-Write Memory? Because that's too hard to pronounce.

    11. To be sure, an 1100 Megabyte disk cost more than $80 in April 1997, but 1100 Megabytes of space on the 6510 Megabyte drive I mentioned earlier would represent a little less than $80 of its total price of $449.
    12. The same disaster would happen if your system were to crash, which is not that unlikely if you're using certain popular PC graphically oriented operating environments whose names start with "W".
    13. Most modern word processors can automatically save your work once in a while, for this very reason. I heartily recommend using this facility; it's saved my bacon more than once.
    14. If you think that last number looks familiar, you're right: It's the number of different values that I said could be stored in a type of numeric variable called a short. This is no coincidence; read on for the detailed explanation.
    15. Paging Dr. Seuss. . .
    16. In the early days of computing, base 8 was sometimes used instead of base 16, especially on machines that used 12-bit and 36-bit registers; however, it has fallen into disuse because almost all modern machines have 32-bit registers.
    17. Either upper or lower case letters are acceptable to most programs (and programmers). I'll use lower case because such letters are easier to distinguish than upper case ones; besides, I find them less irritating to look at.
    18. Please note that the ability to do binary or hexadecimal arithmetic, although valuable in a number of applications, is not essential to further reading in this book.
    19. Each type of CPU has a different set of instructions, so that programs intended for one CPU cannot in general be run on a different CPU. Some CPUs, such as the very popular 80x86 ones from Intel, fall into a "family" of CPUs in which each new CPU can execute all of the instructions of the previous family members. This allows upgrading to a new CPU without having to throw out all of your old programs, but correspondingly limits the ways in which the new CPU can be improved without affecting this "family compatibility".


    Return to the table of contents


    Return to my main page