Objects, Names, and Literals
In This Chapter
What's in a Name? (A Variable Name, That Is)
Tom, Dick42, and _Harry
For example, let's suppose that you have a Web page youre using to sell a product: You're hawking novelty mugs with pictures of your dog wearing a bikini. The prices of these may vary, depending on how many mugs the consumer chooses to purchase at one time. The first variable you'll need to name is the one that refers to the number of mugs ordered. Remember, you can name this variable anything (within the rules above), such as x or bazoo. But, for the sake of programming clarity to the human mind, mugs is probably a good choice for a variable name. (Of course, orders would suffice, too, but you get the concept.)
There is a second variable in the preceding example, and that is the price of each mug. Lets say the single-mug purchase price is $9.95, but two mugs sell for $15.95, and three sell for $22.95. Because the price may vary and cannot be assumed to be constant, it will require a variable name, as well. Again, you could name the price variable mary or y, but something like price or cost seems to be a more reasonable choice.
Full of Value(s)
And the special bonus value is this:
Handing Out Assignments
When making an assignment to a string value, it is vital to place the string within quotation marks (either the double or single kind). Here are two examples:
Boolean values usually represent conditions such as matters in which there can be only a true or false status. A Boolean value assigned to a variable simply looks like this:
And, for the sake of closure, an assignment of the special bonus value, null:
The Plain (Literal) Truth
A value that is not married (assigned) to a variable is known as a literal. This is such a simple concept that it might actually be a tad confusing at first. To wit:
What is the point of any of this? Recall that variables are most often used to track potentially changing values. In some instances, you will want to refer to a value, but it may be one constant amount. For example, perhaps the price of your product is 9.95, and it is always 9.95 in all cases.
Treating Values Like Objects
Note that each property sounds like the variables discussed earlier. That is exactly what they are. Thus, an object is a sort of "supervariable" that contains a set of subvariables. Another example of an object would be an individual person. A person, as an object, contains a set of properties, some of which are useful for our particular applicationsuch as first name, surname, age, gender, and marital status.
Also take note of how objects can fit inside other larger objects. For example, a person object could be part of a family object. A family object might have properties such as size, ethnicity, combined income, and persons (each of which, in turn, possesses its own individual person object properties previously described). Therefore, an object can also be a property of another object. Confusing? Somewhat, but useful as well, as you shall see.
A la Carte versus Prepackaged Objects
So far, you've looked at variables, the values that may be assigned to them, literals, and objects, which are superset groups of variables (known as properties). Here, break for tea and crackers, which are provided in the rec room down the hall. See you in a bit.
The Least You Need to Know
For comments or technical support
for our books and software, select Talk
To order books, call us at 800-716-0044 or 317-228-4366.
© 1997, QUE Corporation, an imprint of Macmillan Publishing USA, a Simon & Schuster Company.