CMPUT101 Lecture Notes - Lecture 4: Dollar Sign
SchoolUniversity of Alberta
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 legally used once the variable has
been bound to it.
Characteristics of variables
– Every variable has two characteristics:
○The scope is the region of program that can use the
○The lifetime is the length of time that the variable exists
– When we declare a variable we give it an identifier (name)
– In Java, an identifier:
○Starts wit a letter, underscore or dollar sign.
○The initial character is followed by zero or more letters,
digits, underscores or dollar signs.
Some other uses for Identifiers
– Identifiers aren’t just used for variables. The following also
○Classes: String, Date, PrintStream
○Messages: toUpperCase, trim, println
○Boolean literals: true, false
Java Identifier Conventions
–Class names start with an upper case letter
–Message names start with a lower case letter
– If an identifier consists of more than one word then the first
letter of subsequent words is capitalized
– A final variable is a variable that can only be bound once and
cannot be re-bound
– Toe declare a final variable in Java, we precede the data type
witht eh keyword, final
– For example:
○Final double MAX_HOURS;
– Once MAX_HOURS is bound, its value cannot be changed – its
value is final.
– The type of every variable has to be declared before the
variable can be used.
– Local variables are declared inside a block of code called a
–Lifetime: is the time that the method is running
–Scope: is the method it is declared in
– If the keyword final is included, the variable can only be bound