setting the stage


Creating a Java program is a bit like making a movie or putting on a play. Every theatrical production needs actors (in Java these are objects), roles the actors play (classes), props the actors handle (variables), names for the actors (reference values), names for the characters that the actors play (reference variables), lines the actors give to each other (messages), stage directions the actors follow (statements), and scenes the actors play out (methods).

A Java program may be only one class (which specifies only one role), or it may be many classes that together specify the behavior of many different types of characters. The Java interpreter, which runs each Java program, is like a combination of stage manager and producer---it's responsible for creating the set, creating the actors, producing the props when needed and disposing of them when not, and remembering everyone's names. We, as Java programmers, are like playwrights and directors put together. Our program's users are the audience.
 
Theater Computer
   
actors objects
roles classes
props variables
actor names reference values
character names reference variables
lines messages
stage directions statements
scenes methods
scripts programs
audience program users
playwright and director programmer
stage manager and producer java interpreter

This analogy only goes so far of course: there's no way to create an actor out of thin air in a play---yet in a Java program we can create as many objects as we want. Also, unlike a play, no Java program needs a stage, lights, or scenery; nor does it need costumes, makeup, orchestras, auditions, rehersals, stagehands, ticket sellers, primadonnas, stage fright, or tantrums. There are, however, many similarities, too.
 
State and Behavior

Just as actors are fundamentally different from props, so too are objects fundamentally different from variables. Props are things that actors fiddle with while on stage; they can be in various states but they can't behave---that's what actors are for.

Without props, the stage gets a bit barren (a Beckett play, perhaps), but props are boring because, usually, they just sit there. A gun in a Chekhov play can be cocked or uncocked, and it can be loaded or unloaded, but it won't hop from one state to the other by itself; it simply sits on the mantlepiece until one of the actors uses it in Act III, Scene IV.

Props usually don't behave on their own---they're picked up, put down, stolen, mislaid, eaten, or slept on by the actors. Similarly, variables in a Java program, like most props in a play, only have state, but no behavior---they continue in one mode or the other until acted on by the objects; they can't act on their own.

Actors, on the other hand, can have both state and behavior. Actors may be hungry or full, asleep or awake, sitting or standing, smoking or yawning---that's part of their state; they may also eat, fall down, snooze, sit, stand, sneeze, speak, and sing---that's part of their behavior. For an actor, "behaving" means "changing state".

Sometimes we'll ask an actor simply to sit still and just hold some props for other actors. Such an actor might just as well be a prop. Most actors though prefer to move about, talk to other actors, get hit on the head, drink, gamble, sleep with other actors, and, of course, have sword fights. They embody the whole life of the play. They're what the play is all about. The same goes for objects in Java; they're what the program is all about.

That's enough overview for now. Now let's look at some of the main entities in a little more detail.
 
The Actors' Props---Java Variables

A variable is like a small box an actor carries around. Each of these boxes has a name, a value, and a type. Its name identifies it, its value is what's inside it, and its type specifies what values we can put into it. Every such box must always have a value, and it can only have one value at a time. The particular set of values it will accept specifies its type, and the value presently in it specifies its state.

There are only four different types of props we need to worry about just now, and we're going to explore three of them right now.

The simplest of these props are boolean variables; each one can hold only one of two values: true or false.

Next come int variables; each one can hold a whole number (or integer), but only within the range from about minus two billion to about plus two billion. If we try to put a whole number outside that range into an int variable the number we end up with will differ wildly from the one we tried to put in.

Then there are double variables; each one can hold a decimal, a number with fractional parts like 3.14159, in the range from about minus 10308 to plus 10308 (that's a 1 followed by 308 zeros). A double variable, however, can't remember more than 15 digits of a decimal. So we can't put the number 9,999,999,999,999,999.0 into a double variable, even though it's in range. If we tried, the 16th decimal place would be unreliable, so we could easily end up with 10,000,000,000,000,000.0.

We can keep any one of the above three types of values in a variable of that type. Thus, a boolean variable can only hold true or false, an int variable can only hold one of about four billion whole numbers, and a double variable can only hold one decimal in a certain range and to a certain precision.

Correspondingly, just as a gun can be in any one of two states---loaded and unloaded---a boolean variable can be in any one of two states, an int variable can be in any one of roughly four billion states, and a double variable can be in any one of roughly 2x10308 states.

In sum, variables are the props we use in our plays. All they can do is hold values, and they can only hold values of their specific type. Besides the above three types of values, however, there is one more type of value that we can put in a fourth kind of variable, and that is actor names.
 
The Actors' Names---Java References

Reference values point to, or name, objects. Although they point to objects and are kept in variables, they are neither objects nor variables. We need them because objects need names. We can't have the characters that the actors play running around calling each other ``Hey, you'' all the time; that would make the play too confusing. And, just as in a play, each character can have many names, or aliases.

It may seem strange to have a whole category devoted just to names, but naming things, and using those names properly, is a big part of programming. It's especially important in Java programs where much of the action often consists of actors sending messages back and forth to each other. Messages can't be sent unless names are first known.

We put reference values in reference variables, just as we put boolean values in boolean variables (int values in int variables, double values in double variables). Reference variables are little boxes with names, types, and values just as before, except that the values in these particular boxes are reference values (that is, actors' names). Thus, unlike boolean or int or double values, the set of values that we can put in a reference variable is theoretically unlimited---we could have any number of actors, and any number of names to call them.

There is a big difference between the names in reference variables and the names of reference variables. The names they store are the names of actors (that is, objects), the names they have are the names of characters those actors play. It's the difference between what's inside a box (an actor's name) and a label on the outside of the box (the name of the character that actor plays).

Further, and again just like the other three kinds of variables, every reference variable has a type. That type might be the same as the type of the object that the reference variable names, or it might be different. We'll see later on what use that is. For now, just remember that everything in Java must have a type.
 
The Actors---Java Objects

Objects are the players on our little stage. They can not only keep values as the four types of variables do, they can also modify those values---and they can ask other objects to modify their own values, too. Thus, objects can have state and behavior.

A variable, whether reference or otherwise, only has state: all it can do is lay around and remember what value it is currently storing out of all the possible values it could be storing. A boolean variable, say, would at any time either be holding the value true or the value false. Which one of the two it's presently holding determines its current state, and that's all. The same goes for int variables, double variables, and reference variables. An object, though, can have many variables, so it can have much more complicated states, and, on top of that, it can also behave: it knows how to do things.

Part of an object's behavior might be to talk to itself or to other objects. It asks objects to do various things by sending messages. Receiving a message might cause those objects to change state and send other messages to yet more objects, one of which might be the original sender. Receiving a reply message might cause the original sender to change state and send out yet more messages. Thus, receivers can become transmitters---and transmitters receivers---perhaps together generating a vast and complex swarm of messages leading to massive state changes in numerous objects. Much of what happens in a well-designed Java program happens through objects sending messages back and forth.
 
The Actors' Scripts---Java Classes

Classes are the scripts that we write and that the actors follow; they are neither variables, nor references, nor objects. They specify what variables the objects possess; what names, types, and values those variables have; how the objects manipulate their variables; what messages the objects send to each other; and what the objects do when they get a message. Like a stage manager or director reading a script, the Java interpreter reads a class to find out how objects of that class should behave.

Each class defines the state and behavior of its own specific type of objects. A small Java program may only need one such type, but a large one may need a universe of many different types of objects, with each type playing a different role and all working together to complete some task.

A play's script usually specifies the actions of many characters in lots of different roles: butlers, tycoons, models, captains, dramatists, girl scouts, lone gunmen. A Java class, however, only specifies the actions of one type of character; that is, one role. So a simple Java program might be the equivalent of a (very boring) play only about butlers, or one only about hairdressers. A complex program, however, may have hundreds of different roles for its objects to play. Each object does exactly as its class tells it, so each object is the personification of a single role. Real actors can play many roles; objects are all role.

Although there are four major kinds of entities in Java, everything revolves around objects. Variables help them remember their state, references help them interact with other objects, and classes describe their state and behavior. They're what the play is all about.

All right, that's enough introduction. It's time to start hacking in Java.


| contents | | next