I am noobie in Smalltak, but I need to understand some things for my thesis. What is exactly happening when creating string or any other object? For example, lets do this:
fruit <- 'apple'
When I try to inspect object fruit, I see it has 5 inst vars. If I had assigned 'pear' to fruit, it would have had 4 inst vars. So interpreter created new instance of bytestring, added required inst vars for every character and assigned them with proper values? I believe there is something more going on, but I can't find it anywhere and I don't have time to properly learn smalltalk. Can you explain it to me, or give me some link where I can find it?
Strings are objects. Objects contain instance variables and respond to messages. In Smalltalk there are basically two kinds of instance variables: named instance variables are referenced by name (like name or phoneNumber in a Person object) and indexed instance variables are referenced by numbers. String uses indexed instance variables.
Consider the following example:
fruit := String new: 5.
fruit at: 1 put: $a;
at: 2 put: $p;
at: 3 put: $p;
at: 4 put: $l;
at: 5 put: $e.
This creates a String with space for 5 characters. It then gets the fruit variable to point to that object. Then it writes 5 characters into the string. The result is the string 'apple'.
Since Strings are so commonly used, the compiler supports a special syntax to create strings at compile time.
fruit := 'apple'
In this example, 'apple' is a String literal. The Smalltalk compiler creates the string when it compiles the line. When you run the line, you will make fruit point to the string 'apple' which has 5 indexed instance variables containing Character objects.