December 22, 2024

Strings and Numbers

 

123 and '123' are instances of two different classes, SmallInteger and ByteString respectively. Different classes implement different behaviour; they perform different tasks and model different aspects of the real world.
If you need to perform mathematical computations, then use number objects.
If you want to output text, or accept a user input via a web interface, you will need to manipulate string objects.
Strings and numbers are probably the most useful objects in computing. Being able to handle them is vitally important. Input from a user usually begins life as a string. If a user types 10 into a text field of a form, what is passed to the OO domain is the string '10'. Smalltalk makes the conversion from string to number a simple process:
'10' asNumber
answers with 10.
Note that the receiver '10' has not changed, but this expression returns a number object that can now be used for further computations.

Similarly:
10 printString
answers with '10'.
The receiver 10 does not change (numbers are immutable), but the expression answers with a string object that can now be put to further use.

« Back to FAQ page

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Up to top of page