CMPUT 114 – Lecture 4 – References: Literals, Variables and
Referring to objects & values
– In a written program, we can’t draw shapes so we need to
have some notation for referring to objects and values called a
– There are different ways to reference objects & values in a
– The simplest way is to use a literal in our code.
– A literal refers to the same object or value at all times.
– You can think of a literal as a nameplate that is automatically
pre-bound to an object or value
– Literals and values cannot be re-bound
– In general, a literal is recognized by the compiler and is
immediately translated into a language value or object
– Common literals in programming languages include
characters, numbers, and strings
– Literals are somewhat restrictive because:
○ Some objects cannot be referred to using literals
○ Sometimes we may want to rebind our nameplates.
– A variable is like a soft nameplate that may be attached to an
object or value and may be re-attached to a different object or
value in the future.
– More than one variable can be attached to the same object or
value at the same time
Java: 4 Kinds or Variables
– local variables (Inside a method)
– static variables (sometimes called “class” variables)
– message parameters (specified in the method params)
– instance variables (specified outside in the class.)
– Every variable must be declared
– The declarations syntax for each kind of variable is different
– A declaration specifies:
○ The amount of memory to be allocated
○ How the data should be internally represented
○ How the data can be l