Expression as Tree
Represent operators and operands as tree nodes

This notional machine represents a source code expression in the form of a tree. This makes the structure of the expression explicit, and it allows visualizing the various steps of expression evaluation. It also allows annotating the various subexpressions with their types.

An expression tree is essentially an abstract syntax tree (AST), focused on just an expression.

Here is an example Java expression:

1 + 2 + "AAA" + 3 + 4

The corresponding expression tree shows not only the expression’s structure but also the types and values of all subexpressions:

75 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.

Misconception
ArithmeticPlusPrecedes
Addition has higher precedence than string concatenation
Misconception
ArrayAccessWithParentheses
Parenthesis are used to access an element in an array
Misconception
ArrayAllocationWithoutNew
Arrays are created without the new keyword
Misconception
ArrayBracketCountIsLength
The number of brackets in an array type or an array initializer corresponds to the length of the array
Misconception
ArrayElementTypeRepeats
The type of a multi-dimensional array is written as T[] T[] T[]
Misconception
ArrayHasLengthMethod
To get the length of an array, one needs to call its length method
Misconception
ArrayInitializerContentsInBrackets
Array initializers list the elements in square brackets
Misconception
ArrayRankIsLength
Array rank and array length are the same thing
Misconception
AssignCompares
= compares two values
Misconception
AssignmentNotExpression
An assignment a=b is not an expression
Misconception
CallOnPrimitive
One can invoke a method on primitive values
Misconception
CallRequiresVariable
One needs a variable to invoke a method
Misconception
CannotChainMemberAccesses
Member accesses cannot be chained together
Misconception
CannotChainMemberToConstructor
Method calls or field accesses cannot be chained to a constructor invocation
Misconception
ChainedMethodsNotCalledFromOutside
Chained methods are all called on the object at the beginning of the chain
Misconception
CharNotNumeric
Char is not a numeric type
Misconception
ComparisonWithBoolLiteral
To test whether an expression is True or False, one must compare it to True or to False
Misconception
ComparisonWithBooleanLiteral
To test whether an expression is true or false, one must compare it to true or to false
Misconception
CompositeExpressionsUntyped
Expressions that consist of multiple parts have no type
Misconception
ConstructorWithoutNew
One can write the constructor name, without new, to instantiate a class
Misconception
EvaluationResultsArePrinted
Evaluating an expression means outputting its result
Misconception
ExpressionAssigns
An expression that reads a variable also updates its value after the evaluation
Misconception
ExpressionsDynamicallyTyped
One has to evaluate an expression to determine its type
Misconception
InlineCallInExpressionTree
The expression tree of an expression involving a call inlines the call's computation of the returned value
Misconception
InlineVariableInExpressionTree
The expression tree of an expression involving a variable inlines the variable's definition
Misconception
IntegerDivisionToRational
Dividing two integers can produce a rational number
Misconception
LiteralNoExpression
A literal is not an expression
Misconception
LiteralString
When passing a literal string as argument to a method, no quotes are needed
Misconception
MapToBooleanWithConditionalOperator
To map a boolean expression to a boolean, a conditional operator is necessary
Misconception
MapToBooleanWithTernaryOperator
To map a boolean expression to a bool, a ternary conditional operator is necessary
Misconception
MultiReferenceVariable
A reference variable can point to multiple objects
Misconception
MultiValueVariable
A variable can contain more than one value
Misconception
MultidimensionalArray
A multi-dimensional array is one thing
Misconception
NoAtomicExpression
Expressions must consist of more than one piece
Misconception
NoCallOnStringLiteral
One cannot invoke methods on String literals
Misconception
NoCastIfSameSize
If a variable is at least as big (bit-width) as a value, then no cast is needed to a assign the value to the variable
Misconception
NoFieldInheritance
An object contains only the fields declared in its class
Misconception
NoFloatLiterals
There are no float literals
Misconception
NoImplicitWidening
Smaller types are never automatically converted into bigger ones without an explicit cast
Misconception
NoJaggedArrays
Multi-dimensional arrays have a rectangular shape
Misconception
NoLongLiterals
There are no long literals
Misconception
NoMethodInheritance
Subclasses inherit fields but not methods
Misconception
NoShortCircuit
&& and || always evaluate both operands
Misconception
NoShortCircuit
and/or always evaluate both operands
Misconception
NoSingleLogicAnd
& is only a bitwise AND
Misconception
NoStringToString
One cannot invoke toString() on a String
Misconception
NullPointerExceptionCompileTime
NullPointerExceptions are detected at compile time
Misconception
NumericToBooleanCoercion
Numeric types can be coerced to boolean
Misconception
ObjectsMustBeNamed
A variable is needed to instantiate an object
Misconception
OnlyInnermostArrayElements
Only the elements of the innermost array of a multi-dimensional array are accessible
Misconception
OutOfBoundsElementsAreNull
Out-of-bounds array elements are null
Misconception
OutsideInFunctionNesting
Nested function calls are invoked outside in
Misconception
OutsideInMethodNesting
Nested method calls are invoked outside in
Misconception
ParenthesesOnlyIfArgument
() are optional for method calls without arguments
Misconception
PrimitiveVariablesDynamicallyTyped
The type of a primitive variable depends on its value
Misconception
PrivateAccessibleInSubclass
Private members of a superclass are accessible from a subclass
Misconception
RationalLiterals
Rational fractions are literals
Misconception
ReferenceToBooleanCoercion
Every reference type can be coerced to boolean
Misconception
ReferenceToIntegerConversion
One can cast between references and ints
Misconception
RightToLeftChaining
Chained accesses are invoked from right to left
Misconception
SelfNoExpression
The name self is not an expression
Misconception
SingleQuoteString
String literals can be in single quotes
Misconception
StringLengthField
One can know the length of a String object by accessing its length field
Misconception
StringLiteralNoObject
One needs to call the String constructor to get a String object from a literal
Misconception
StringPlusStringifiesExpression
String concatenation stringifies non-String operand expressions
Misconception
StringRepetitionOperator
The multiplication operator can repeat a String a number of times
Misconception
SuperAlwaysHasParentheses
To call a method on a superclass, parentheses are needed after the keyword super
Misconception
TargetTyping
The type of a numerical expression depends on the type expected by the surrounding context
Misconception
ThisAssignable
One can assign to this
Misconception
ThisCanBeNull
this can be null
Misconception
ThisChildInCallExpression
Instance method or constructor call nodes in expression trees have a child labeled "this"
Misconception
ThisInConstructorIsNull
In a constructor, this is null
Misconception
ThisNoExpression
The name this is not an expression
Misconception
ToStringPrints
Invoking toString() prints something
Misconception
VariablesHoldExpressions
= stores an expression in a variable

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.