Next: , Previous: , Up: Getting started  

6.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 22 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. 23

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.



Which table? This is determined by the type of the object. An object has a type, known as the class to which it belongs. Each class has a table of methods. For the object we created, it is known as a member of the String class. So we go to the table associated with the String class.


Actually, the message printNl was inherited from Object. It sent a print message, also inherited by Object, which then sent printOn: to the object, specifying that it print to the Transcript object. The String class then prints its characters to the standard output.