Next: , Previous: What happened, Up: Getting started

6.1.4 Doing math

A similar piece of code prints numbers:

       1234 printNl

Notice how we used the same message, but have sent it to a new type of object—an integer (from class Integer). The way in which an integer is printed is much different from the way a string is printed on the inside, but because we are just sending a message, we do not have to be aware of this. We tell it to printNl, and it prints itself out.

As a user of an object, we can thus usually send a particular message and expect basically the same kind of behavior, regardless of object's internal structure (for instance, we have seen that sending printNl to an object makes the object print itself). In later chapters we will see a wide range of types of objects. Yet all of them can be printed out the same way—with printNl.

White space is ignored, except as it separates words. This example could also have looked like:

                   1234 printNl

However, gnu Smalltalk tries to execute each line by itself if possible. If you wanted to write the code on two lines, you might have written something like:


From now on, we'll omit printNl since gnu Smalltalk does the service of printing the answer for us.

An integer can be sent a number of messages in addition to just printing itself. An important set of messages for integers are the ones which do math:

        9 + 7

Answers (correctly!) the value 16. The way that it does this, however, is a significant departure from a procedural language.