29.11.2014 Views

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

Smalltalk and Object Orientation: an Introduction - Free

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

ounded round to nearest truncate round down<br />

In general, the arithmetic operators available in <strong>Smalltalk</strong> are essentially the sa me as in <strong>an</strong>y other<br />

l<strong>an</strong>guage. For example, there is addition, subtraction, multiplication <strong><strong>an</strong>d</strong> division operators (+, -,*,/).<br />

There are also comparison functions <strong><strong>an</strong>d</strong> truncation functions all of which are summarized in Table 7.1.<br />

7.3 Characters <strong><strong>an</strong>d</strong> strings<br />

7.3.1 Characters<br />

Just like numbers, characters in <strong>Smalltalk</strong> are inst<strong>an</strong>ces of <strong>an</strong> associated class. In this case the class<br />

Character. Again we will consider this class <strong><strong>an</strong>d</strong> the operations it provides in greater detail later. For<br />

the moment we will consider what characters look like. In <strong>Smalltalk</strong>, a single character is defined by<br />

prefixing it with the $ (dollar) sign. For example:<br />

$a $Z $@ $1 $$<br />

All the above specify a single character, in this case the characters a, b, @, 1 <strong><strong>an</strong>d</strong> $.<br />

7.3.2 Strings<br />

Strings in <strong>Smalltalk</strong> are part of the Collection class hierarchy. As such they are made up of<br />

individual elements in a similar m<strong>an</strong>ner to strings in C. However, this is the only similarity between C<br />

strings <strong><strong>an</strong>d</strong> Smalltal k strings. <strong>Smalltalk</strong> strings do not need to be terminated by a null character, nor<br />

should they be treated as arrays of characters. In <strong>Smalltalk</strong>, a string should be treated as <strong>an</strong> object in its<br />

own right which responds to <strong>an</strong> appropriate r<strong>an</strong>ge of messages (e. g. for m<strong>an</strong>ipulating or extracting<br />

substrings).<br />

In <strong>Smalltalk</strong>, a string is defined by one or more characters placed between single quotes. For<br />

example:<br />

'John Hunt' 'Tuesday' 'dog'<br />

Note the use of single quotes rather th<strong>an</strong> the double quotes used in some o ther l<strong>an</strong>guages (e.g. C <strong><strong>an</strong>d</strong><br />

Ada). This c<strong>an</strong> be the source of much confusion <strong><strong>an</strong>d</strong> frustration when <strong>an</strong> apparently correct piece of<br />

code will not work. Remember that double quotes indicate a comment in <strong>Smalltalk</strong>. Thus, the following<br />

code will compile, but will generate a run time error:<br />

a := "John Hunt".<br />

This is because there is nothing to assign to the variable a as the comment “John Hunt” does not<br />

return a value. Also be wary of assuming that a string containing a single character is equivalent to that<br />

single character. It is not. For example:<br />

'a' /= $a<br />

The string ‘a’ <strong><strong>an</strong>d</strong> the character $a are inst<strong>an</strong>ces of different classes, the fact that the string contains only<br />

one character is just a coincidence. This c<strong>an</strong> be particularly confusing for C programs as ‘a’ ind icates<br />

the character a in C.<br />

7.3.3 Symbols<br />

Symbols are special strings which are always unique in the system. They do not respond to m<strong>an</strong>y of<br />

the usual string m<strong>an</strong>ipulation messages, but they c<strong>an</strong> be more efficient for some tasks th<strong>an</strong> strings. A<br />

symbol is indicated by a preceding hash (#). For example:<br />

#john #Week #System42<br />

70

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!