Represent variables as boxes, organized in different memory areas
Stack Heap Global Diagram
This notional machine represents the state of a program by showing three separate parts of memory: the stack with its stack frames and their local variables, the heap with heap objects and their instance variables, and the global area with static variables.
Every variable is shown as a rectangle containing its current value. Variables are annotated with their name. In statically typed languages, variables are also annotated with their type.
62 Misconceptions Expressible in this Notional Machine
A notional machine focuses on a subset of the syntax and semantics of a programming language. The following misconceptions are related to the language features expressible in this notional machine.
AddMemberAtRuntime
Set of class members can change at runtimeArrayHasLengthMethod
To get the length of an array, one needs to call its length methodArrayListIsArray
ArrayLists are arraysArrayRankIsLength
Array rank and array length are the same thingArraysGrow
Arrays can grow dynamicallyAssignCompares
= compares two valuesAssignmentCopiesObject
Assignment copies the objectBoxedNull
Passing null to a wrapper class constructor creates an object representing the absence of a valueCallWithoutFrame
A method invocation does not necessarily allocate a stack frameCallerFrameContainsCalleeFormal
Stack frame of caller includes variables for callee's formal parametersConstructorAllocates
The constructor allocates the objectConstructorParameterIsField
Formal constructor parameters are equivalent to instance variablesEqualityOperatorComparesListIdentities
(list a) = (list b) compares the identities of list a and list bEqualityOperatorComparesObjectsValues
o==p compares the objects referred to by variables o and pEqualsComparesReferences
o.equals(p) compares the references stored in the variables o and pExpressionAssigns
An expression that reads a variable also updates its value after the evaluationFinalReferenceImpliesImmutability
An object referred to by a final variable is an immutable objectForEachVariableIsElement
One can assign to the variable of an enhanced for statement to store a value in the corresponding array or collection elementFrameIsClassInstance
A stack frame is the same as an instance of a classImmutableRequiresFinalParameters
Immutable classes need final constructor/method parametersInitCreates
__init__ must create a new objectLocalVariablesAutoInitialized
Local variables are automatically initializedMethodAsField
Each object contains its own special fields for all of its methodsMultiReferenceVariable
A reference variable can point to multiple objectsMultiValueVariable
A variable can contain more than one valueMultidimensionalArray
A multi-dimensional array is one thingMustInitializeFieldInConstructor
Constructors must assign values to all fieldsNoFieldInheritance
An object contains only the fields declared in its classNoFieldlessObjects
Objects without instance variables can't existNoJaggedArrays
Multi-dimensional arrays have a rectangular shapeNoLocalVariables
There are no local variablesNullIsObject
null is an objectNullPointerExceptionCompileTime
NullPointerExceptions are detected at compile timeObjectLabeledWithConstructorSignature
In stack and heap diagrams, an object on the heap is labeled like a stack frame of a constructor callObjectLabeledWithVariableName
In stack and heap diagrams, an object on the heap is labeled with the name of the variable that refers to itObjectsMustBeNamed
A variable is needed to instantiate an objectOnlyInnermostArrayElements
Only the elements of the innermost array of a multi-dimensional array are accessiblePrimitiveIsObject
Primitive values are heap objectsPrimitiveVariablesDynamicallyTyped
The type of a primitive variable depends on its valuePrivateFieldsImplyImmutability
A class where all fields are private is immutablePrivateFromOtherInstance
An object cannot access private members of other objects of the same classPrivateFromStatic
Static methods cannot access private members of instances of same classPrivateMeansFinal
A private field cannot be changedRecursiveActivationsShareFrame
Recursive calls of a method share a stack frameReferenceIntoStack
References can point into the stackReferenceLabeledWithDynamicType
In stack and heap diagrams, reference variables are labeled with the types of the objects they refer toReferenceToVariable
References can point to variablesStackTraceIsCallHistory
A stack trace is the sequence of previously called methodsStaticFrameInGlobals
Stack frame of static method calls lives in the global memory areaStringLengthField
One can know the length of a String object by accessing its length fieldStringLiteralNoObject
One needs to call the String constructor to get a String object from a literalSubtypeCompatibleWithSupertype
A variable of a subtype can reference an object of a supertypeSuperclassObjectAllocated
When instantiating an object of a subclass, an object of a superclass is also allocatedSupertypeIncompatibleWithSubtype
A variable of a supertype cannot reference an object of a subtypeThisAsField
this is a special field in the objectThisAssignable
One can assign to thisThisCanBeNull
this can be nullThisExistsInStaticMethod
this is a local variable, also in static methodsThisInConstructorIsNull
In a constructor, this is nullUntypedVariables
Variable declarations don't need a typeVariablesHoldExpressions
= stores an expression in a variableVariablesHoldObjects
A variable of a reference type contains a whole object