Changing strings

C. Gable Watts CGableWatts at
Mon Apr 22 18:35:23 UTC 2002

Hi Jen,

It isn't that string literals are unique in the image, it's that equal string literals are identical in a METHOD.  To see the difference look at the following 3 lines of code in a Workspace:

string1 := 'Squeak'.
string2 := 'Squeak'.
string1 == string2 

If you printIt all 3 lines of code together, the answer is true (the two variable refer to THE SAME String object) because the whole thing was compiled as one method.  But it you doIt the first two lines each individually, then printIt the last line, the answer is false (the two variables refer to 2 DIFFERENT String objects).

When a method or expression is compiled, if the same string literal appears multiple times in the method the SAME String object is reused for the literal in the method (for space efficiency).  But the compilation of a DIFFERENT method (or expression) will use a different String object.

- In some Smalltalk's separate String objects are made for each string literal in a method.
- In some Smalltalk's (like Squeak at the moment) equal string literals in the same method share the same String object.  
- In some Smalltalk's, a string literal in a method is compiled as an immutable (StringLiteral) object (which cannot be changed with at:put: for example).  

This latter way is the way it really should work. It should not be possible to modify string literals.  So that attempting to execute the expression
	'squeak' at: 6 put: $l.
would give a 'Message not understood: at:put:' generated by the LiteralString object, 'squeak', because it does not understand the message at:put: (because its an immutable (unchangeable) object).

But, instead, it is just considered inappropriate (bad programming practice) to modify a String literal and it is considered risky to modify a String that was passed to you that might be a string literal.  A case of a common practise arising over the decades to avoid a problem that should have been fixed "lower down".  It should be impossible to do it (modify a string literal) but instead we all just try to avoid doing it.  :-)

Carl Gable Watts

At 1:33 PM -0700 4/21/02, jennyw wrote:
>I'm just getting back into Smalltalk after 10 years, so please pardon me
>if this is a well-known topic.
>a := 'squeak'.
>'squeak' at: 6 put: $l.
>I've noticed that the above code results in a equaling 'squeal'. This 
>seems kind of strange to me.  It seems that literal strings, like symbols, 
>are unique?  I'm not certain, but I don't think all Smalltalks are like 
>I did notice that:
>a := 'squeak'.
>b:= 'squeal'.
>'squeak' at: 6 put: $l.
>a == b.
>returns false, so I guess it may not be a problem very often.  
>Nonetheless, I find it disturbing that if a an instance variable is 
>initialized using a string literal, and if a user knows what that string 
>literal is, then the user can change the value of that instance variable 
>without an accessor.
>Any comments?

More information about the Squeak-dev mailing list