Back: Tutorial
Up: Tutorial
Forward: Starting Smalltalk
 
Top: GNU Smalltalk User's Guide
Contents: Table of Contents
Index: Class index
About: About this document

5.1 Getting started

5.1.1 Starting up Smalltalk  
5.1.2 Saying hello  
5.1.3 What actually happened  But how does it say hello?
5.1.4 Doing math  Smalltalk too can do it!
5.1.5 Math in Smalltalk  But in a peculiar way of course...


5.1.1 Starting up Smalltalk

Assuming that GNU Smalltalk has been installed on your system, starting it is as simple as:
 
   $ gst
the system loads in Smalltalk, and displays a startup banner like:
 
     Smalltalk Ready

     st>

You are now ready to try your hand at Smalltalk! By the way, when you're ready to quit, you exit Smalltalk by typing control-D on an empty line.


5.1.2 Saying hello

An initial exercise is to make Smalltalk say "hello" to you. Type in the following line (printNl is a upper case N and a lower case L):
 
   'Hello, world' printNl !
The system then prints back 'Hello, world' to you.(13)


5.1.3 What actually happened

The front-line Smalltalk interpreter gathers all text until a '!' character and executes it. So the actual Smalltalk code executed was:
 
   'Hello, world' printNl

This code does two things. First, it creates an object of type String which contains the characters "Hello, world". Second, it sends the message named printNl to the object. When the object is done processing the message, the code is done and we get our prompt back. You'll notice that we didn't say anything about printing ing the string, even though that's in fact what happened. This was very much on purpose: the code we typed in doesn't know anything about printing strings. It knew how to get a string object, and it knew how to send a message to that object. That's the end of the story for the code we wrote.

But for fun, let's take a look at what happened when the string object received the printNl message. The string object then went to a table (14) which lists the messages which strings can receive, and what code to execute. It found that there is indeed an entry for printNl in that table and ran this code. This code then walked through its characters, printing each of them out to the terminal. (15)

The central point is that an object is entirely self-contained; only the object knew how to print itself out. When we want an object to print out, we ask the object itself to do the printing.


5.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         !

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.


5.1.5 Math in Smalltalk

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)) printNl !
   (8 - (4 + 1)) printNl !
   (5 + 4) printNl !
   (2/3 + 7) printNl !
   (2 + 3 * 4) printNl !
   (2 + (3 * 4)) printNl !




This document was generated on May, 12 2002 using texi2html