Introduction to Computer Science - Java

        Writing computer programs is similar to giving instructions to human beings.     A computer program is a set of instructions that a computer can execute (carry out) one after the other.     Each of these instructions is very simple.     By simple I mean that it is a very small (discrete) action for the computer to do.     By combining tens, hundreds or even millions of such instructions programmers can get computers to do very complex things, like computing the salaries of employees and the like.     But each instruction does very little.     Only by combining many instructions can complex actions be done.     A set of such instructions is called a program.     These instructions are also called the program code.     These instructions are executed in order (one after the other going downward).   Another way of saying "in order" is saying "in sequence"     Therefore we can say a program is a set of code which is executed in sequence.    

        Unlike human beings, computers cannot figure out anything by themselves.     This means the programmer must spell out every detail of what he wants the computer to do.     But be careful, there is a saying:     What does a computer do?     A computer doesn't do what you want it to do, it does what you tell it to do .     Therefore, be very carful what you tell it to do.     A computer program could be compared to a cooking recipie for people who have no idea how to cook.     A normal human recipie might have a statement like:

mix the ingredients in a bowl

A cooking recipie that a computer could understand would be a lot more detailed. For the same human instruction above, a computer would need something like the following:

a bowl is a round container
a cabinet is a shelf with doors
a counter is a flat area 3 feet higher than the ground
remove bowl from cabinet
put bowl on counter
put first ingredient in bowl
put second ingredient in bowl
put third ingredient in bowl
put fourth ingredient in bowl
remove mixing spoon from drawer
stir contents of bowl with mixing spoon
stir contents of bowl with mixing spoon
stir contents of bowl with mixing spoon
stir contents of bowl with mixing spoon
stir contents of bowl with mixing spoon

        Since the computer knows very little itself , we have to tell it everything we want it to do.     We cannot assume it will figure anything out for itself.     For example, if the programmer wants to compute the sum of 7 and 8 in Java he would first need to create a variable to store the sum in.     In the next instruction he could put the sum into that variable.     A variable is a place in the computer's memory (RAM) where information can be stored.     Something like the way an empty shoebox can store shoes, computer memory can store numbers or letters.     To do the above in Java, I would write the following:

int nachumsSum;
nachumsSum = 7+8;


        The above is two instructions.     How do I tell the computer that this is two separate instructions?     The semi-colon ; indicates the instruction is over.     By having two semi-colons I indicate that I have two instructions (also called commands).     The fact that they are on two separate lines in not relevent.     I could have two commands on one line, or one command over two lines.     The computer does not care about lines.     Human readers do care about lines.     Human readers find it easier to read separate lines, so programmers usually write each command on a separte line for clarity.    

The first instruction (a. k. a. "command") is

int nachumsSum;

When the computer reads this line it does the following:

Assigns the name "nachumsSum" to a variable (a place in computer memory) .    

        The word int is a special word in Java which indicates that a memory place should be assigned a name.     There are other such special words in Java which mean different things.     These special words are called reserved words.     The name we assigned to the variable is "nachumsSum".     This is not a reserved word, but is a variable name of my choosing.     From now on in the program the name nachumsSum will refer to this variable.     ( I cannot have two variables with the same name. ) Only at the time I am assigning the name to the variable do I use the command int when I later use the variable I simply write nachumsSum , not int nachumsSum.    

        The second command:

nachumsSum = 7+8;

assigns (gives) the value on the right side of the = sign to the left side.

In other words, it gives the variable the value of 7+8.     Since 7+8 is 15, the variable "nachumsSum" gets the value 15.     Variables are similar to what is refered to in algebra as a variables.   ( We will see later that there is a difference between algebraic variables and programming variables, but for now think of them as the same thing. )     They are signs (a letter or a short word) which can receive different values.     Because they can receive different values, and therefore change in value from time to time, they are called variables.     The value of a variable varies.     It is therefore logical to say that its value is variable.     Hence, it is called a "variable")     Programmers usually call this "assigning a value to a variable."     Because this instruction assigns a value to a variable, it is also called an assignment.     The = sign is called the assignment operator because the operation it performs is assignment.     (Don't call it "equals" because this is a different concept.)    The term operator should be added to the terms you have learnt.    

       In contrast to variables, numbers like 7 or 17000 or 1.1416 are fixed, that is, their value logically cannot change.    If 7 changes to 8, then it is no longer 7.    Numbers such as these are therefore called constants because their value is constant; it never changes.    We can assign a constant ( like 7 ) to a variable ( like "nachumsum" ) but we cannot do the reverse.   ( If you think about it, it does not make any sense at all to assign a variable to a number.     In fact, whatever is on the left side of the assignment operator ( = ) must be a variable and never a constant. )     Get used to the terms variable and constant and remember the difference between them.

        We have seen that first we must assign a name to a varaible.    After this we can assign a value to the variable.    These two operations together create a variable and give it an initial value.    This process is a common one and is called initialization.   

Terms in this chapter: instruction, command, sequence, code, reserved word, variable, value, constant, assignment, operator, and initialization.


© Nachum Danzig September 2003