1.1 Introduction

This book is about problem-solving using the Java programming language. It is not primarily a book about how computers work. Having some basic ideas about how computers are organized will, however, be useful in understanding how to use a computer to solve problems. Thus, before we start looking at problem-solving, we are going to take a brief look at the structure of a computer system and how Java can be used with a computer system. This section contains a large number of terms that may be unfamiliar. Do not worry too much, for now, about the terminology. Read through the section trying to understand as much as you can and then, once you have written a few Java programs, come back and re-read it. It should make more sense once you have actually used a computer system a few times.
       A computer system can be divided into two main parts: hardware and software. The hardware consists of all the equipment that is involved in operating a computer. It can include keyboards, screens, CD drives, speakers, modems, and printers. The software consists of sets of instructions, called programs, that are used to tell the computer what tasks it is to perform.



The central processing unit (CPU) is the heart of a computer. It consists of two main parts: a control unit and an arithmetic-logic unit (ALU). The control unit interprets the instructions of the programs and sends commands to various other parts of the computer so that the instructions are executed. When commanded by the control unit, the arithmetic-logic unit performs arithmetic and makes very simple logical decisions such as determining whether or not two numbers have the same value.
       The memory of the computer is the part that stores both the instructions that the CPU is to perform and the data on which these instructions are to be performed. We can think of the memory as an electronic chalkboard in which the computer keeps notes on both what it is to do and the values it is to use. Memory is organized into numbered storage locations, each of which is capable of holding a small amount of information. The number assigned to each storage location is called its address. The CPU can examine the contents of these locations, move data from one location to another, send data out of memory to a device such as a printer, and bring new data into memory from a device such as a keyboard.
       To communicate with the world, each computer must have some input and output devices, collectively referred to as I/O devices. A small computer may have only a couple of input devices - a keyboard and a mouse, and one output device - a screen. Larger computer systems, however, may have many other input devices such as cameras, digitizing pads, and microphones along with many output devices such as printers and speakers.
       Devices known as units are found with virtually all computer systems. These devices are sometimes classified as I/O devices, and sometimes as an integral part of the computer system itself. They include disk drives, tapes, and CD drives. They are used very much like memory, with some important differences.

      1.They are capable of holding a much larger volume of data.

      2.Accessing information in them takes far longer than accessing data held in memory.

      3.The information stored in these units is retained in them from one session on the computer to another. This is not generally true of memory; each time that a          computer is turned off, the contents of memory are usually lost.

      4.The information in auxiliary storage is usually organized in the form of files. A file can contain a set of instructions or a collection of data. The way in which the          files are organized varies from one computer system to another and even from one storage unit to another. The local system documentation will have to be          consulted for details.


       As we have already noted, every computer system must have software as well as hardware before it can do any computing. Each computer's CPU uses instructions encoded electronically in memory. This code is called, reasonably enough, machine code. Although it is possible to write programs directly in machine code, this process has a number of disadvantages. It is very easy for a programmer to make a mistake when writing these programs, they are very difficult to follow, and, because different machines use different machine codes, a program written in the machine code of one computer will have to be completely rewritten in order to work on some other computer.
       For these reasons, most programming is now done using high-level languages. Programs in these languages are reasonably easy to write, understand, and modify. There are many such high-level languages in use today; Java is such a language.
       A program written in a high-level language cannot be performed directly by the hardware of a computer. Instead, another program, usually called a compiler, must be used to translate the high-level language into machine code. The original program in the high-level language is called the source program while the corresponding machine code is called the object program. Thus, to have a computer perform the instructions of a program written in a high-level language, we must go through two stages:

       1.Compilation: Here the compiler is brought into memory and its instructions are performed. The instructions of the compiler cause the computer to read the           source program and translate it into the corresponding object program.

       2.Execution: The machine code instructions of the object program are performed.

       With Java, the process is slightly more complicated. At the compilation stage, a Java source program is not compiled into the machine code of a particular machine. Instead, all source programs are compiled into the same machine language - for a machine that does not exist! This compiled code is called Java byte code. In order to run a compiled Java program on a real machine, another program, called an interpreter, can be used to translate from byte code to the machine code for the actual machine on which the program is being run. This program is called the Java Virtual Machine (JVM). Since different computer systems have different machinelanguages, each computer system on which we want to run Java must have its own JVM. Luckily, this is almost always the case; JVM interpreters have been written for a wide range of computer systems.
      The advantage of this added complexity is that the writer of a Java program need not be concerned about the machine on which the program will be run. For example, if a Java program is written on an Apple computer and compiled into byte code, it can then be sent out over a network and run on any other machine connected to the network. It is this feature that has led the creators of the language at Sun Microsystems to characterize Java as a language in which we "write once, run anywhere".

Exercise 1.1

  1. Name the principal parts of

    (a) a computer system,
    (b) a computer,
    (c) a CPU.

  2. Which part of a computer would perform each of the following tasks?

    (a) add 2533 and 4178
    (b) print a line of text
    (c) determine whether one number is larger than a second number
    (d) store the result of a calculation
    (e) send an item from memory to an output device

  3. How is information organized in auxiliary storage units?


  4. Why are programs rarely written in machine code?


  5. Explain the difference between a source program and an object program.


  6. What is the primary difference between the object code produced by most compilers and the byte code produced by the Java compiler?