Skip to main content
Engineering LibreTexts

5.2: Everything is an Object

  • Page ID
    36355
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    The mantra “everything is an object” is highly contagious. After only a short while working with Smalltalk, you will start to be surprised at how this rule simplifes everything you do. Integers, for example, are truly objects, so you can send messages to them, just as you do to any other object.

    3+4            → 7 "send '+4' to 3, yielding 7"
    20 factorial   → 2432902008176640000 "send factorial, yielding a big number" 
    

    The representation of 20 factorial is certainly different from the representation of 7, but because they are both objects, none of the code — not even the implementation of factorial — needs to now about this.

    Perhaps the most fundamental consequence of this rule is the following:

    Classes are objects too.

    Furthermore, classes are not second-class objects: they are really first-class objects that you can send messages to, inspect, and so on. This means that Squeak is a truly reflective system, which gives a great deal of expressive power to developers.

    Deep in the implementation of Smalltalk, there are three different kinds of objects. There are (1) ordinary objects with instance variables that are passed by references, there are (2) small integers that are passed by value, and there are (3) indexable objects like arrays that hold a contiguous portion of memory. The beauty of Smalltalk is that you normally don’t need to care about the differences between these three kinds of object.


    This page titled 5.2: Everything is an Object is shared under a CC BY-SA 3.0 license and was authored, remixed, and/or curated by Andrew P. Black, Stéphane Ducasse, Oscar Nierstrasz, Damien Pollet via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.