Search code examples
smalltalk

VisualWorks Cincom Smalltalk Array Initialization Differences


|temp|
temp := Array new: 5.
temp at: 1 put: 10.

Gives no error.

|temp|
temp := #(1 2 3 4 5).
temp at: 1 put: 10.

Gives error

What is the difference b/w the two ways of initializing arrays?


Solution

  • (1 2 3 4 5) is an immutable array.

    It cannot be modified.

    Array new: 10 is a non immutable array. It can be modified.