Tutorial 2 - Java Expressions, Values and Variables
Tutorial 2 - Java Expressions, Values and Variables
April 4, 2012
Recall: Method Bodies & “Statements”
• Method bodies consist of
√ Comments (mostly ignored by “build”)
√ Variable Declarations
– Statements (most involving “Expressions”)
What is a Value?
• A value is a single quantity (that cannot be
further evaluated)
• Values can be of different “types” (where a
type describes the legal universe of possible
values)
– Integers (e.g. 2, -10)
– Floating point values (e.g. 2.5, 3.14159, 0.0, 1.0)
– Characters (e.g. ‘a’, ‘b’)
– Boolean (e.g. true/false)
– Reference to objects that are instances of classes
(e.g. Person, String, Main, Simulation, etc.)
Java “Expressions”
• A Java Expression computes (“evaluates to”) a
value, typically based on other values of the same
of different types
– This is like a “formula” to compute a value
– For years, you have likely been writing expressions in
algebra, using them in Excel, in calculators, etc.
• Examples: “2*5”, “1.0/3”, “(a+b)*c”, “a>b”,
“this.getConnectedAgent(i).name”
• In the process of computing the value, it may cause
some changes to “program state” (e.g. change the
value of a variable”
• Assignment expression
In most• places
CallingAnyLogic
a methodwants a value, we can give it a Java Expression.
Common Java Expressions
• Literal (3.5, 1, “my string”, { 1, 2.71, 3.14, 0}, null)
• Comparison (a>b,a==b, a<=b)
• Mathematical Operators (+,-,/,*) Can be “overloaded”
to mean other things (e.g. + as concatenation)
• “Dereferencing”: Looking up field or value b in a
reference to an object a: (a.b) (a is a reference to an
instance of a class; b is a name of a field or method
associated with the class of a, and thus with the object)
• Ternary operator: (predicate ? a : b)
• Potentially causes changes (Side effecting)
– Assignment (a=b) Left hand side is some location (variable,
field, etc.) and variants (a++, ++a, a+=2, a*=5)
– Method call (function call): this.get_Main()
Additional Common Operators
• Boolean expressions
– a&&b (logical and), a||b (logical or), !a (logical not)
• Indexing: a[20], a[getConnectionsNumber()-1]
– Must value preceding must denote an array
• Method call: f(2,3)
• For strings
strA+strB (concatenates strings)
Reading Java Expressions
• Generally, expressions are calculated from
“inside out”, and left-to-right
– e.g. a.getConnectedAgent(i).getName().length
• Expressions are routinely “strung together” in
this way (e.g. where the left components
return values used by the right components)
Variable Declarations
• Most java variable denotes location that contains a
value (exceptions: constant vars, type parameters)
• Variables are associated with “types”
– The types describe the sort of values that a variable can
contain (the set of possible values), e.g.
• double: Double precision floating point numbers
• int: (positive & negative): Integer values within some range
• boolean: A dichotomous value, holding “true”, or “false”
• String: A (reference to a) text sequence
• When we “declare” a variable, we indicate its name
& type – and possibly an initial value
• In Java, the value associated with a variable can
change over time (as location holds different values)
Example Variable Declarations
Main
this population
After a.mother=m
Person
m age[int]: 40
mother [Person]:
isMale
Person [boolean]:False
age[int]: 4
mother [Person]:
isMale [boolean]:True
a
Main
this population
After
a.mother=this.get_Main().population[2]
Person
m age[int]: 40
mother [Person]:
isMale
Person [boolean]:False
age[int]: 4
mother [Person]:
isMale [boolean]:True
a
Main
this population