Previous: Doing math, Up: Getting started
In this case, what happened was that the object 9
(an
Integer), received a +
message with an argument of 7
(also an Integer). The +
message for integers then caused
Smalltalk to create a new object 16
and return it as the
resultant object. This 16
object was then given the
printNl
message, and printed 16
on the terminal.
Thus, math is not a special case in Smalltalk; it is
done, exactly like everything else, by creating objects, and
sending them messages. This may seem odd to the Smalltalk
novice, but this regularity turns out to be quite a boon:
once you’ve mastered just a few paradigms, all of the language
“falls into place”. Before you go on to the next
chapter, make sure you try math involving *
(multiplication),
-
(subtraction), and /
(division) also. These
examples should get you started:
8 * (4 / 2) 8 - (4 + 1) 5 + 4 2/3 + 7 2 + 3 * 4 2 + (3 * 4)