www.delorie.com/gnu/docs/smalltalk/gst_46.html   search  
Buy GNU books!

GNU Smalltalk User's Guide

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.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:
   printNl         !

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) printNl !

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

  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003